I use plyr
for anything that involves split-apply-combine. In this case, we split the data by Customer
and apply the length-unique function on Product
, then combine the results
require(plyr)
ddply(df, .(Customer), transform, num.products = length(unique(Product)))
Customer Product num.products
1 1 Chocolate 3
2 1 Candy 3
3 1 Soda 3
4 2 Chocolate 1
5 2 Chocolate 1
6 2 Chocolate 1
7 3 Insulin 2
8 3 Candy 2
Bonus in case you want a smaller summary dataframe out of this.
ddply(df, .(Customer), summarize, num.products = length(unique(Product)))
Customer num.products
1 1 3
2 2 1
3 3 2