我有一个包含customerid和列表的数据框.我想合并那些与同一客户有关的清单.
library(plyr)
subsets <- list(c("a", "d", "e"), c("a", "b", "c", "e"))
customerids <- c(1,1)
transactions <- data.frame(customerid = customerids,subset =I(subsets))
> transactions
customerid subset
1 1 a, d, e
2 1 a, b, c, e
Run Code Online (Sandbox Code Playgroud)
如果我想将子集与ddply合并,我会得到一个扩展的结果
> ddply(transactions, .(customerid), summarise, subset=Reduce(union,subset))
customerid subset
1 1 a
2 1 d
3 1 e
4 1 b
5 1 c
Run Code Online (Sandbox Code Playgroud)
虽然我希望所有的结果都在一排.
你可以这样做:
ddply(transactions, .(customerid), function(x)
data.frame(subset=I(list(unlist(x$subset)))))
Run Code Online (Sandbox Code Playgroud)
编辑:我不确定我是否遵循了您的评论。但是,如果您只想在每个customeridfor中使用唯一值subset,那么:
ddply(transactions, .(customerid), function(x)
data.frame(subset=I(list(unique(unlist(x$subset))))))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
183 次 |
| 最近记录: |