-1

I have the following R dataframe

df1=data.frame(x = c(1,1,2,2,2,3), y = c("f","g","g","h","i","f"), z=c(6,7,5,2,1,5))  

      x y z
    1 1 f 6
    2 1 g 7
    3 2 g 5
    4 2 h 2
    5 2 i 1
    6 3 f 5

and I need to obtain

df2=data.frame(x = c(1,2,3), f=c(6,0,5), g=c(7,5,0), h=c(0,2,0),i=c(0,1,0))  

      x f g h i
    1 1 6 7 0 0
    2 2 0 5 2 1
    3 3 5 0 0 0

I tried using dcast from reshape2

df3=dcast(df1,x~y,length)

which yields

  x f g h i
1 1 1 1 0 0
2 2 0 1 1 1
3 3 1 0 0 0

which is not exactly what I need.

Thanks for your help!

UPDATE

I realize this question was already asked and a complete answer can be found here.

By the way Akrun's answer is exactly what I need in a clear format.

Community
  • 1
  • 1

1 Answers1

1

We don't need to specify the fun.aggregate if the values in the 'z' column needs to be populated for each combination of 'x' and 'y' (assuming that there are no duplicate combinations for 'x' and 'y'

dcast(df1, x~y, value.var='z', fill=0)
#  x f g h i
#1 1 6 7 0 0
#2 2 0 5 2 1
#3 3 5 0 0 0

Or using spread from library(tidyr)

spread(df1, y, z, fill=0)
akrun
  • 874,273
  • 37
  • 540
  • 662