我有一个包含用户和产品列的数据框。
> head(df1)
Users Product Code
[1,] "Alice" "Soap"
[2,] "Alice" "Shampoo"
[3,] "Alice" "Toothpaste"
[4,] "Bob" "Cup"
[5,] "Bob" "Spoon"
[6,] "Charlie" "Towel"
是否可以将其更改为
> head(df1)
Users Product Code
[1,] "Alice" "Soap, Shampoo, Toothpaste"
[2,] "Bob" "Cup, Spoon"
[3,] "Charlie" "Towel"
使用任何直接的简单命令。
我目前通过吸引每个用户并检查他购买的产品来做到这一点。有没有简单的方法呢?
item_set = list()
for(user_id in 1:length(user_code))
{
item_set[[user_id]] = products$product_code[products$customerID == user_id]
}