我有这样的数据:
ID=c("A123","A123","A123","A123","B456","B456","B456","C789","C789")
item=c("bread", "butter", "milk", "eggs", "meat","milk", "peas", "peas", "meat")
df=data.frame(cbind(ID, item))
ID item
1 A123 bread
2 A123 butter
3 A123 milk
4 A123 eggs
5 B456 meat
6 B456 milk
7 B456 peas
8 C789 peas
9 C789 meat
我已经变成了像这样的sparce矩阵:
df_g = dcast(df,ID~item,fill=0, length)
df_g
ID bread butter eggs meat milk peas
1 A123 1 1 1 0 1 0
2 B456 0 0 0 1 1 1
3 C789 0 0 0 1 0 1
我怎样才能将df_g-data送到这个“篮子”形式?:
ID basket
A123 bread,butter,eggs,milk
B456 meat,milk,peas
C789 meat,peas
我知道将df直接转换为“baskets”形式很容易。然而,这不是解决方案。将数据从df转换为df_g时,数据的顺序会发生变化,这就是为什么我需要从df_g -data获取“baskets”-form的原因。