比较 R 中的数据集 [英] Compare datasets in R
问题描述
我已在 CSV 文件中收集了一组交易,格式为:
I have gathered a set of transactions in a CSV file of the format:
{Pierre, lait, oeuf, beurre, pain}
{Paul, mange du pain,jambon, lait}
{Jacques, oeuf, va chez la crémière, pain, voiture}
我打算做一个简单的关联规则分析,但首先我想从每个事务中排除不属于 ReferenceSet = {lait, oeuf, beurre, pain}
的项目.
I plan to do a simple association rule analysis, but first I want to exclude items from each transactions which do not belong to ReferenceSet = {lait, oeuf, beurre, pain}
.
因此,在我的示例中,我的结果数据集将是:
Thus my resulting dataset would be, in my example :
{Pierre, lait, oeuf, beurre, pain}
{Paul,lait}
{Jacques, oeuf, pain,}
我确信这很简单,但很想阅读一些建议/答案来帮助我.
I'm sure this is quite simple, but would love to read suggestions/answers to help me a bit.
推荐答案
另一个答案参考 %in%
,但在这种情况下 intersect
更方便(你可能想要也可以查看 match
-- 但我认为它与 %in%
记录在同一位置) -- 使用 lapply
和 intersect
我们可以把答案写成一条直线:
Another answer references %in%
, but in this case intersect
is even handier (you may want to look at match
, too -- but I think it's documented in the same place as %in%
) -- with lapply
and intersect
we can make the answer into a one-liner:
数据:
> L <- list(pierre=c("lait","oeuf","beurre","pain") ,
+ paul=c("mange du pain", "jambon", "lait"),
+ jacques=c("oeuf","va chez la crémière", "pain", "voiture"))
> reference <- c("lait", "oeuf", "beurre", "pain")
答案:
> lapply(L,intersect,reference)
$pierre
[1] "lait" "oeuf" "beurre" "pain"
$paul
[1] "lait"
$jacques
[1] "oeuf" "pain"
这篇关于比较 R 中的数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!