0

In R, is there a quick way to go from something like this (data.frame):

P1        A      X
P2        B      X
P3        C      X
P4        D      X
P1        E      X
P2        A      X
P5        F      X
P6        C      X

To something like this (table):

          A       B       C       D       E        F
P1        X                               X
P2        X       X
P3                        X
P4                                X
P5                                                 X
P6                        X

For sample data:

df=as.data.frame(cbind(c('P1','P2','P3','P4','P5','P6'),c('A','B','C','D','E','F'),rep('X',6)))

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • 1
    I suppose `dcast(df, V1 ~ V2)` would work. Note there will be "NA" instead of a missing string – Mike H. May 05 '17 at 14:15
  • Your sample df is not equal to your example ... `df=data.frame("V1" = c('P1','P2','P3','P4','P1','P2','P5','P6'), V2 = c('A','B','C','D','E','A','F','C'), V3 = rep('X',8))` and then `table(df$V1, df$V2)` in base – Evan Friedland May 05 '17 at 14:47

0 Answers0