Ddply to create a merge list

I have a data frame that contains customerid and a list. I would like to combine a list related to the same client.

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 

If I want to combine subsets with ddply, I get an extended result

 > 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 

while I would expect all the results in 1 row.

+7
r plyr
source share
1 answer

You can do something like this:

 ddply(transactions, .(customerid), function(x) data.frame(subset=I(list(unlist(x$subset))))) 

Edit: I'm not sure I'm following your comments. But if you want only unique values ​​in each customerid for a subset , then:

 ddply(transactions, .(customerid), function(x) data.frame(subset=I(list(unique(unlist(x$subset)))))) 
+4
source share

All Articles