在R中创建新数据框(Creating New Data Frame in R)

编程入门 行业动态 更新时间:2024-10-24 20:13:44
在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.00

but 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.00

Please 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 1

Here 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

更多推荐

本文发布于:2023-08-01 16:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1361559.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:建新   中创   数据   Frame   Data

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!