[R] cross tabulate variables by subject id

Marianne Promberger marianne.promberger at kcl.ac.uk
Mon Nov 29 10:18:23 CET 2010


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



More information about the R-help mailing list