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

hadley wickham h.wickham at gmail.com
Tue May 5 22:20:03 CEST 2009


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/




More information about the R-help mailing list