[R] reshape package cast() function

Ista Zahn izahn at psych.rochester.edu
Mon Jan 25 18:26:29 CET 2010


OK, I think I figured it out (each level of id corresponds to only one
level of variable in m.test).

Thanks,
Ista
On Mon, Jan 25, 2010 at 4:59 PM, Ista Zahn <izahn at psych.rochester.edu> wrote:
> Hi all,
> I think I'm cracking up. Please help me understand why I'm getting
> different results with m.test and m.test2 in the example below.
>
>> library(reshape)
> Loading required package: plyr
>>
>> m.test <- data.frame(id = factor(rep(1:10, 2)), variable=rep(c("var1","var2"),10), value=rnorm(20))
>> cast(m.test, ...~variable, value="value") ## cast is aggregating even though I have ... in the formula.
> Aggregation requires fun.aggregate: length used as default
>   id var1 var2
> 1   1    2    0
> 2   2    0    2
> 3   3    2    0
> 4   4    0    2
> 5   5    2    0
> 6   6    0    2
> 7   7    2    0
> 8   8    0    2
> 9   9    2    0
> 10 10    0    2
>>
>> test2 <- data.frame(id=factor(1:10), var1=rnorm(10), var2=rnorm(1))
>> m.test2 <- melt(test2, id="id")
>> cast(m.test2, ...~variable, value="value") ## this is the result I want. I don't understand why I'm not getting the same thing for m.test.
>   id        var1      var2
> 1   1  0.19829479 -0.642097
> 2   2  0.37971900 -0.642097
> 3   3 -0.23557248 -0.642097
> 4   4  0.12969246 -0.642097
> 5   5 -0.12019995 -0.642097
> 6   6  0.09744247 -0.642097
> 7   7 -1.24467558 -0.642097
> 8   8  1.69505449 -0.642097
> 9   9  1.60714925 -0.642097
> 10 10  0.11961170 -0.642097
>
> Thanks,
> --
> Ista Zahn
> Graduate student
> University of Rochester
> Department of Clinical and Social Psychology
> http://yourpsyche.org
>



-- 
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org



More information about the R-help mailing list