我有一个类似这样的客户数据集
ID | Item |
1 | A |
1 | B |
1 | C |
2 | A |
2 | B |
2 | B |
3 | A |
3 | B |
3 | C |
4 | A |
4 | B |
4 | C |
5 | A |
5 | B |
在R中,如何找到客户中购买频率最高的商品组合?
我尝试过
df %>%
group_by(ID,Item) %>%
mutate(n = n()) %>%
group_by(Item) %>%
top_n(3, Item) %>%
select(-n)
但是它似乎不起作用。我可以得到dplyr解决方案吗?
dput:
structure(list(ID = c(1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5,
5), Item = c("A", "B", "C", "A", "B", "B", "A", "B", "C", "A",
"B", "C", "A", "B")), row.names = c(NA, -14L), class = c("tbl_df",
"tbl", "data.frame"))
答案 0 :(得分:2)
一个dplyr
选项可能是:
df %>%
group_by(ID) %>%
summarise(Item = paste(sort(unique(Item)), collapse = "")) %>%
count(Item)
Item n
<chr> <int>
1 AB 2
2 ABC 3