[R] Compiling a contingency table of counts by case

hadley wickham h.wickham at gmail.com
Fri Sep 22 18:04:02 CEST 2006


> I have asked a similar question before but this time
> the problem is somewhat more involved. I have the
> following data:
>
> case;name;x
> 1;Joe;1
> 1;Mike;1
> 1;Zoe;1
> 2;Joe;1
> 2;Mike;0
> 2;Zoe;1
> 2;John;1
> 3;Mike;1
> 3;Zoe;0
> 3;Karl;0
>
> I would like to count the number of "case"
> in which any two "name"
>
> a. both have "x=1",
> b. the first has "x=0" - the second has "x=1",
> c. the first has "x=1" - the second has "x=0",
> d. both have "x=0",

One way is to use the reshape package:

dat <- read.delim("clipboard", sep=";")
dat <- rename(dat, c(x=value))
cast(dat, name ~ case)

(which doesn't give you exactly what you want, but I think might be
more illuminating)

Hadley



More information about the R-help mailing list