Using dcast to widen a data frame

r, reshape, reshape2

Solution

I'm not sure why you're putting a "sum" function in there. Try this instead:

> dcast(z, id ~ item, value.var="freq")
  id x y z
1  1 4 3 2
2  2 4 4 1

Problem

I have the following data frame: ``` z <- data.frame( item=letters[rep(24:26,2)], freq=c(4,3,2,4,4,1), id=rep(1:2,each=3) ) item freq id x 4 1 y 3 1 z 2 1 x 4 2 y 4 2 z 1 2 ``` The data frame is unique on every `id,item` combination. I would it to be like this: ``` id x y z 1 4 3 2 2 4 4 1 ``` This looks like a fairly simple transform but I can't seem to get it to work. Here is what I have tried (`z` is the name of the data frame): ``` dcast(z,id ~ item,sum) ``` and it returns this: ``` id x y z 1 1 1 1 2 2 2 2 ``` What am I doing wrong?

Original source