I am trying to add a column to an existing dataframe, such that the column defines the number of different products, each user has bought. A toy example is
Customer Product
1 Chocolate
1 Candy
1 Soda
2 Chocolate
2 Chocolate
2 Chocolate
3 Insulin
3 Candy
Where the output should be
Customer Product #Products
1 Chocolate 3
1 Candy 3
1 Soda 3
2 Chocolate 1
2 Chocolate 1
2 Chocolate 1
3 Insulin 2
3 Candy 2
I would like to do this without a for loop, since I have millions of rows, and it would take forever. I have used data.table and other methods in order to just get the number of products for each customer, but I don't know how to easily add this as a column to the existing dataframe.
Thanks in advance!