[R] re shape package - use one cast() instead of many

jwg20 jason.gullifer at gmail.com
Tue May 5 22:55:14 CEST 2009


Thanks for your help! I wasn't sure what the margins variable did, but I'm
beginning to understand. I'm almost there, but with my data (and with ff_d)
I tried to margin over two variable names, however it only does one of them.
So with ff_d I set margins=c("treatment","variable"); however I only ever
get 1_(all) 2_(all) and 3_(all)... never something like (all)_painty. (This
also happens for margins=TRUE)

To further exemplify, with my data I have the call to cast() as:
cast(data.melted,Subject~CogStat+Animacy,mean,margins=c("CogStat","Animacy"))

which results in:

  Subject     COG_a    COG_i COG_(all)    nCOG_a    nCOG_i nCOG_(all)
1     100  794.3333 676.5556  728.0833  810.7778  798.4103   800.7292
...

I would like additionally to get (all)_i (all)_a. It seems to just apply the
margin to the first part of the formula before "+" (i.e. I can change it to
Animacy+CogStat and get a_COG,    a_nCOG,   a_(all), etc.)


hadley wrote:
> 
> On Tue, May 5, 2009 at 3:03 PM, jwg20 <jason.gullifer at gmail.com> wrote:
>>
>> I have a data set that I'm trying to melt and cast in a specific way
>> using
>> the reshape package. (I'll use the ff_d dataset from reshape so I don't
>> have
>> to post a toy data set here. )
>>
>> Lets say I'm looking for the interaction of treatment with each type of
>> "variable" in ff_d. Using the command below gets me this. Subject will
>> get a
>> column and each treatment type by each variable will also get a column
>> with
>> values for each.
>>
>> cast(ff_d, subject~treatment+variable)
>>   subject 1_potato 1_buttery 1_grassy 1_rancid 1_painty 2_potato
>> 2_buttery
>> ....  3_painty
>> 1        3       18        18       18       18       18       18      
>>  18
>> ....   18
>> ...
>>
>> Now, if I want to look at just the  the values for each variable by
>> subject
>> I can run the following command.
>> cast(ff_d, subject~variable)
>>   subject potato buttery grassy rancid painty
>> 1        3     54      54     54     54     54
>> ...
>>
>> What I'm wondering now, is run one cast() call and get both of these in
>> one
>> data.frame? Essentially, the values for each separate "condition" and
>> interactions between them? cast() doesn't let me repeat variable names as
>> that's what I first tried.  Right now, i'm just running two separate
>> cast()
>> calls and cbinding/merging them together. Is there a better way?
> 
> Have a look at the margins argument.
> 
> Hadley
> 
> 
> -- 
> http://had.co.nz/
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 
> 

-- 
View this message in context: http://www.nabble.com/reshape-package---use-one-cast%28%29-instead-of-many-tp23394916p23395785.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list