Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating New Data Frame in R

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

like image 722
jbest Avatar asked Dec 19 '22 09:12

jbest


1 Answers

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
like image 97
jazzurro Avatar answered Dec 27 '22 03:12

jazzurro