[R] Simple reshape problem I am completely missing

John Kane jrkrideau at inbox.com
Tue Jul 24 19:17:08 CEST 2012

I seem to be doing something really stupid or missing something really obvious  but what?

I have a simple three column data.frame that I would like to reshape to wide preferably using reshape2.

An example from http://stackoverflow.com/questions/9617348/reshape-three-column-data-frame-to-matrix looked perfect except I wanted a data frame but it seemed okay. I just changed acast to dcast and it seems fine.

Stackoverflow example:
tmp <- data.frame(x=gl(2,3, labels=letters[24:25]),
                  y=gl(3,1,6, labels=letters[1:3]), 
dd  <-  dcast(tmp, x~y, value.var="z")

My Example: Does NOT work

md2  <-  structure(list(group = structure(c(1L, 1L, 1L, 2L, 2L, 3L, 3L, 
4L, 4L, 4L, 5L, 5L, 6L, 6L, 7L, 7L, 7L, 8L, 8L), .Label = c("X1", 
"X2", "X3", "X4", "X5", "X6", "X7", "X8"), class = "factor"), 
    tps = structure(c(7L, 12L, 14L, 4L, 8L, 9L, 16L, 6L, 7L, 
    11L, 6L, 15L, 10L, 13L, 3L, 4L, 5L, 1L, 2L), .Label = c("A", 
    "C", "D", "E", "G", "I", "L", "M", "N", "P", "Q", "R", "S", 
    "T", "V", "Y"), class = "factor"), sum = c(0.914913196595112, 
    0.0367565080432513, 0.0483302953616366, 0.982727803634948, 
    0.0172721963650521, 0.0483302953616366, 0.951669704638363, 
    0.89764100023006, 0.0850868034048879, 0.0172721963650521, 
    0.951669704638363, 0.0483302953616366, 0.963243491956749, 
    0.0367565080432513, 0.89764100023006, 0.0540287044083034, 
    0.0483302953616366, 0.982727803634948, 0.0172721963650521
    )), .Names = c("group", "tps", "sum"), row.names = c(NA, 
-19L), class = "data.frame")

dcast(md2,  group ~ tps , value.vars  = "sum")

What am I doing wrong?

John Kane
Kingston ON Canada

FREE ONLINE PHOTOSHARING - Share your photos online with your friends and family!
Visit http://www.inbox.com/photosharing to find out more!

More information about the R-help mailing list