2

I have a data frame with columns Users and Products.

> head(df1)
     Users         Product Code   
[1,] "Alice"        "Soap"   
[2,] "Alice"        "Shampoo"   
[3,] "Alice"        "Toothpaste"
[4,] "Bob"          "Cup"   
[5,] "Bob"          "Spoon"    
[6,] "Charlie"      "Towel"

Is it possible to change this to

> head(df1)
  Users         Product Code   
[1,] "Alice"   "Soap, Shampoo, Toothpaste"   
[2,] "Bob"     "Cup, Spoon"   
[3,] "Charlie" "Towel"

using any direct simple commands.

I currently do this by taking each user and checking what products he bought. Is there any simple way to do this?

item_set = list()
for(user_id in 1:length(user_code))
{
  item_set[[user_id]] = products$product_code[products$customerID == user_id]
}
Perseus14
  • 467
  • 1
  • 6
  • 19
  • It seems like you have a `matrix` instead of `data.frame`. If it is a data.frame, you can do `aggregate(Product_Code ~Users, df1, FUN = toString)` – akrun Nov 01 '17 at 12:08
  • 1
    It worked, thanks!! I was storing the output in a matrix but the input is in a data.frame – Perseus14 Nov 01 '17 at 12:14

0 Answers0