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

2005-06-04 Thread Sander Oom

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 165   1
2.1  2 1 2 165   2
3.1  3 2 1 165   3
4.1  4 2 2 165   4
5.1  1 1 1 178   5
6.1  2 1 2 142   6
7.1  3 2 1 197   7
8.1  4 2 2 197   8
1.2  1 1 1 297   1
2.2  2 1 2 297   2
3.2  3 2 1 268   3
4.2  4 2 2 297   4
5.2  1 1 1 298   5
6.2  2 1 2 297   6
7.2  3 2 1 298   7
8.2  4 2 2 298   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@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   [EMAIL PROTECTED]
Web www.oomvanlieshout.net/sander

__
R-help@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


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

2005-06-03 Thread Sander Oom

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.

Thanks,

Sander.



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   [EMAIL PROTECTED]
Web www.oomvanlieshout.net/sander

__
R-help@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


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

2005-06-03 Thread Charles Plessy
On Fri, Jun 03, 2005 at 04:33:44PM +0200, 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

I think that reshape is the tool you are looking for.

-- 
Charles

__
R-help@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


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

2005-06-03 Thread Thomas Lumley

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 165   1
2.1  2 1 2 165   2
3.1  3 2 1 165   3
4.1  4 2 2 165   4
5.1  1 1 1 178   5
6.1  2 1 2 142   6
7.1  3 2 1 197   7
8.1  4 2 2 197   8
1.2  1 1 1 297   1
2.2  2 1 2 297   2
3.2  3 2 1 268   3
4.2  4 2 2 297   4
5.2  1 1 1 298   5
6.2  2 1 2 297   6
7.2  3 2 1 298   7
8.2  4 2 2 298   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@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