[R] How to 'de-cross' a table?

Sander Oom slist at oomvanlieshout.net
Sat Jun 4 14:13:24 CEST 2005


Hi Thomas,

Your code works perfectly!

Thanks a lot,

Sander.



Thomas Lumley wrote:
> On Fri, 3 Jun 2005, Sander Oom wrote:
> 
>> Dear R users,
>>
>> I have received a table in the following format:
>>
>> id  a   b  c1  c2  d1  d2
>> 1   1   1  65  97  78  98
>> 2   1   2  65  97  42  97
>> 3   2   1  65  68  97  98
>> 4   2   2  65  97  97  98
>>
>> Factors of the design are: a, b, and e, where e has levels c and d. The
>> levels c and d then have 2 replicates (r) each.
>>
>> Now I would like to get:
>>
>> id  a   b   e   r  value
>> 1   1   1   c   1  65
>> 2   1   1   c   2  97
>> 3   1   1   d   1  78
>> 4   1   1   d   2  98
>>
>> Any suggestions on how to tackle this? I'm not sure what the 'task' is
>> called, so struggle to effectively search the web or R help.
>>
> 
> reshape() is the probably function.  It seems you need to use it twice, 
> for the two levels of uncrossing
>> d1<-reshape(d, direction="long", 
> varying=list(c("c1","d1"),c("c2","d2")), 
> v.names=c("rep1","rep2"),timevar="r", times=c("c","d"))
>> d1
>     id a b r rep1 rep2
> 1.c  1 1 1 c   65   97
> 2.c  2 1 2 c   65   97
> 3.c  3 2 1 c   65   68
> 4.c  4 2 2 c   65   97
> 1.d  1 1 1 d   78   98
> 2.d  2 1 2 d   42   97
> 3.d  3 2 1 d   97   98
> 4.d  4 2 2 d   97   98
>> reshape(d1, direction="long", varying=list(c("rep1","rep2")), 
> v.names="value",idvar="tmp", timevar="r")
>     id a b r value tmp
> 1.1  1 1 1 1    65   1
> 2.1  2 1 2 1    65   2
> 3.1  3 2 1 1    65   3
> 4.1  4 2 2 1    65   4
> 5.1  1 1 1 1    78   5
> 6.1  2 1 2 1    42   6
> 7.1  3 2 1 1    97   7
> 8.1  4 2 2 1    97   8
> 1.2  1 1 1 2    97   1
> 2.2  2 1 2 2    97   2
> 3.2  3 2 1 2    68   3
> 4.2  4 2 2 2    97   4
> 5.2  1 1 1 2    98   5
> 6.2  2 1 2 2    97   6
> 7.2  3 2 1 2    98   7
> 8.2  4 2 2 2    98   8
> 
> You can then delete the `tmp` variable if you don't need it.
> 
> An easier way to uncross would be with stack()
>> stack(d[,4:7])
>    values ind
> 1      65  c1
> 2      65  c1
> 3      65  c1
> 4      65  c1
> 5      97  c2
> 6      97  c2
> 7      68  c2
> 8      97  c2
> 9      78  d1
> 10     42  d1
> 11     97  d1
> 12     97  d1
> 13     98  d2
> 14     97  d2
> 15     98  d2
> 16     98  d2
> 
> but you lose the other variables.
> 
> 
>     -thomas
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
> 


-- 
--------------------------------------------
Dr Sander P. Oom
Animal, Plant and Environmental Sciences,
University of the Witwatersrand
Private Bag 3, Wits 2050, South Africa
Tel (work)      +27 (0)11 717 64 04
Tel (home)      +27 (0)18 297 44 51
Fax             +27 (0)18 299 24 64
Email   sander at oomvanlieshout.net
Web     www.oomvanlieshout.net/sander




More information about the R-help mailing list