在R中创建新数据框(Creating New Data Frame in R)
我在R中有这种格式的数据
customer_key item_key units 2669699 16865 1.00 2669699 16866 1.00 2669699 46963 2.00 2685256 55271 1.00 2685256 43458 1.00 2685256 54977 1.00 2685256 2533 1.00 2685256 55011 1.00 2685256 44785 2.00但我想获得独特的head_key作为列,我希望我的其他变量名称是item_key中的唯一值,它们的值将是这样的单位
customer_key '16865' '16866' '46963' '55271' '43458' '54977' '2533' 2669699 1.00 1.00 1.00 0.00 0.00 0.00 0.00 2685256 0.00 0.00 0.00 1.00 1.00 1.00 2.00请帮助我转换数据以进行聚类分析
I have data in this format in R
customer_key item_key units 2669699 16865 1.00 2669699 16866 1.00 2669699 46963 2.00 2685256 55271 1.00 2685256 43458 1.00 2685256 54977 1.00 2685256 2533 1.00 2685256 55011 1.00 2685256 44785 2.00but I want to get the unique head_key as column and I want my the other variables name be the unique values in item_key and their value would be the units like this
customer_key '16865' '16866' '46963' '55271' '43458' '54977' '2533' 2669699 1.00 1.00 1.00 0.00 0.00 0.00 0.00 2685256 0.00 0.00 0.00 1.00 1.00 1.00 2.00Please help me transform my data for cluster analysis
最满意答案
这是一种方法。
library(tidyr) spread(mydf,item_key, units, fill = 0) # customer_key 2533 16865 16866 43458 44785 46963 54977 55011 55271 #1 2669699 0 1 1 0 0 2 0 0 0 #2 2685256 1 0 0 1 2 0 1 1 1Here is one way.
library(tidyr) spread(mydf,item_key, units, fill = 0) # customer_key 2533 16865 16866 43458 44785 46963 54977 55011 55271 #1 2669699 0 1 1 0 0 2 0 0 0 #2 2685256 1 0 0 1 2 0 1 1 1更多推荐
发布评论