Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ddply for creating the union of lists

Tags:

r

plyr

I have a data frame which contains a customerid, and a list. I would like to merge those list pertaining to the same customer.

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 merge the subsets with ddply, I get an expanded 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 have expected all the results in 1 row.

like image 455
nicolas Avatar asked Jul 15 '13 21:07

nicolas


1 Answers

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 follow your comments. But if you want just unique values within each customerid for subset then:

ddply(transactions, .(customerid), function(x) 
            data.frame(subset=I(list(unique(unlist(x$subset))))))
like image 118
Arun Avatar answered Oct 16 '22 15:10

Arun