[R] cross tabulate variables by subject id

2010-11-29 Thread Marianne Promberger
Dear list,

I have data like this:

dat1 - data.frame(subject=rep(1:10,2),
   cond1=rep(c(A,B),each=5),
   cond2=rep(c(C,D),each=10),
   choice=sample(0:1,10,replace=TRUE))

I would like to compare subjects' choice for (cond1==A 
cond2==C) vs (cond1==A  cond2==D), using mcnemar.test

The ?mcnemar.test example has the data in a matrix:

 Performance
2nd Survey
1st Survey   Approve Disapprove
  Approve794150
  Disapprove  86570


So for my case, I need something like:

   Choice
 AC
AD   0  1
  0 ...
  1

Where ... would be the sum of subjects who answered 0 or 1 to AC
and/or AD respectively.

I can get the first step by making an extra variable:

dat1$condnew - paste(dat1$cond1,dat1$cond2,sep=)

although I am sure there are more elegant ways, and especially, I am
stumped how to fill in the cells of the table.

Thanks,

Marianne

-- 
Marianne Promberger PhD, King's College London
http://promberger.info
R version 2.12.0 (2010-10-15)
Ubuntu 9.04

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


Re: [R] cross tabulate variables by subject id

2010-11-29 Thread Michael Bedward
Hi Marianne,

How about this...

ac.ad - unstack(dat1, choice ~ cond1:cond2)[, c(A.C, A.D)]
acad.xtab - with(ac.ad, table(A.C, A.D))

Michael


On 29 November 2010 20:18, Marianne Promberger
marianne.promber...@kcl.ac.uk wrote:
 Dear list,

 I have data like this:

 dat1 - data.frame(subject=rep(1:10,2),
                   cond1=rep(c(A,B),each=5),
                   cond2=rep(c(C,D),each=10),
                   choice=sample(0:1,10,replace=TRUE))

 I would like to compare subjects' choice for (cond1==A 
 cond2==C) vs (cond1==A  cond2==D), using mcnemar.test

 The ?mcnemar.test example has the data in a matrix:

     Performance
            2nd Survey
 1st Survey   Approve Disapprove
  Approve        794        150
  Disapprove      86        570


 So for my case, I need something like:

   Choice
         AC
 AD       0      1
  0     ...
  1

 Where ... would be the sum of subjects who answered 0 or 1 to AC
 and/or AD respectively.

 I can get the first step by making an extra variable:

 dat1$condnew - paste(dat1$cond1,dat1$cond2,sep=)

 although I am sure there are more elegant ways, and especially, I am
 stumped how to fill in the cells of the table.

 Thanks,

 Marianne

 --
 Marianne Promberger PhD, King's College London
 http://promberger.info
 R version 2.12.0 (2010-10-15)
 Ubuntu 9.04

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


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