[R] cross tabulate variables by subject id
michael.bedward at gmail.com
Mon Nov 29 12:40:57 CET 2010
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))
On 29 November 2010 20:18, Marianne Promberger
<marianne.promberger at kcl.ac.uk> wrote:
> Dear list,
> I have data like this:
> dat1 <- data.frame(subject=rep(1:10,2),
> 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:
> 2nd Survey
> 1st Survey Approve Disapprove
> Approve 794 150
> Disapprove 86 570
> So for my case, I need something like:
> AD 0 1
> 0 ...
> 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.
> Marianne Promberger PhD, King's College London
> R version 2.12.0 (2010-10-15)
> Ubuntu 9.04
> R-help at r-project.org mailing list
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help