[R] Count cases by indicator

Adrian DUSA adi at roda.ro
Sat Dec 9 17:08:03 CET 2006


Dear Serguei and Andy,

I was away for a few days so I appologize for this late reply. I cannot help 
but noticing Serguei's problem is somewhat suited to the QCA package.
For example, the 2^k combinations can be created simply with:

library(QCA)
cmat <- createMatrix(9)


As to the problem itself, the solution would be:

m <- as.data.frame(matrix(df$x, ncol=9, byrow=TRUE))
rownames(m) <- levels(df$case)
m$OUT <- 0
truthTable(m, outcome="OUT", show.cases=TRUE)

The result seconds Andy's result.

If Serguei wants the entire matrix, then use the "inside" argument:
truthTable(df, outcome="OUT", inside=TRUE)

I hope it helps,
Adrian


On Monday 04 December 2006 15:24, Liaw, Andy wrote:
> I might be missing something, but the data you showed don't seem to
> match your expectation.  Firstly, 111111111 in binary is 511 in decimal,
> so your "coordinates" are off by 1.  Secondly, for the data you've
> shown, the matrix equivalent look like:
>
> m <- matrix(df$x, ncol=9, byrow=TRUE)
> rownames(m) <- levels(df$cases)
> print(m)
>
>          [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
> 093/0188    0    0    1    0    1    1    1    1    1
> 093/0206    0    0    0    0    0    0    0    0    0
> 093/0216    0    1    1    1    1    1    0    1    1
> 093/0305    0    1    1    1    1    1    1    1    1
> 093/0325    0    0    0    0    0    0    0    0    0
> 093/0449    0    0    0    0    0    0    0    0    0
> 093/0473    0    0    1    1    1    1    1    1    1
> 093/0499    0    0    1    1    1    1    1    1    1
>
> The counts of unique occurances are:
>
> table(do.call(paste, c(as.data.frame(m), sep="")
>
> 000000000 001011111 001111111 011111011 011111111
>         3         1         2         1         1
>
> which do not agree with yours.
>
> If I understood what you wanted, I would do:
>
> R> table(rowSums(matrix(2^(0:8) * df$x, ncol=9, byrow=TRUE)))
>
>   0 446 500 508 510
>   3   1   1   2   1
>
> Andy
>
>
> From: Serguei Kaniovski
>
> > Hi,
> >
> > In the data below, "case" represents cases, "x" binary
> > states. Each "case" has exactly 9 "x", ie is a binary vector
> > of length 9.
> >
> > There are 2^9=512 possible combinations of binary states in a
> > given "case", ie 512 possible vectors. I generate these in
> > the order of the decimals the vectors represent, as:
> >
> > cmat<-as.matrix(expand.grid(rep(list(0:1),9)))
> > cmat<-cmat[nrow(cmat):1,ncol(cmat):1]
> >
> > "cmat" contains the binary vectors as rows.
> >
> > QUESTION: I would like to know how often each of the 512
> > vectors occurs in "case".
> >
> > With these data, the output should be a vector with 2^9=512
> > coordinates, having 2,2,1,3, as, respectively, the coordinate
> > number 129, 193, 449, 512, and zeros in all other coordinates.
> >
> > Thank you for your help,
> > Serguei
> >
> > df<-read.delim("clipboard",sep=";")
> >
> > DATA:
> > case;x
> > 093/0188;0
> > 093/0188;0
> > 093/0188;1
> > 093/0188;0
> > 093/0188;1
> > 093/0188;1
> > 093/0188;1
> > 093/0188;1
> > 093/0188;1
> > 093/0206;0
> > 093/0206;0
> > 093/0206;0
> > 093/0206;0
> > 093/0206;0
> > 093/0206;0
> > 093/0206;0
> > 093/0206;0
> > 093/0206;0
> > 093/0216;0
> > 093/0216;1
> > 093/0216;1
> > 093/0216;1
> > 093/0216;1
> > 093/0216;1
> > 093/0216;0
> > 093/0216;1
> > 093/0216;1
> > 093/0305;0
> > 093/0305;1
> > 093/0305;1
> > 093/0305;1
> > 093/0305;1
> > 093/0305;1
> > 093/0305;1
> > 093/0305;1
> > 093/0305;1
> > 093/0325;0
> > 093/0325;0
> > 093/0325;0
> > 093/0325;0
> > 093/0325;0
> > 093/0325;0
> > 093/0325;0
> > 093/0325;0
> > 093/0325;0
> > 093/0449;0
> > 093/0449;0
> > 093/0449;0
> > 093/0449;0
> > 093/0449;0
> > 093/0449;0
> > 093/0449;0
> > 093/0449;0
> > 093/0449;0
> > 093/0473;0
> > 093/0473;0
> > 093/0473;1
> > 093/0473;1
> > 093/0473;1
> > 093/0473;1
> > 093/0473;1
> > 093/0473;1
> > 093/0473;1
> > 093/0499;0
> > 093/0499;0
> > 093/0499;1
> > 093/0499;1
> > 093/0499;1
> > 093/0499;1
> > 093/0499;1
> > 093/0499;1
> > 093/0499;1
> > --
> > ___________________________________________________________________
> >
> > Austrian Institute of Economic Research (WIFO)
> >
> > Name: Serguei Kaniovski			P.O.Box 91
> > Tel.: +43-1-7982601-231			Arsenal Objekt 20
> > Fax:  +43-1-7989386			1103 Vienna, Austria
> > Mail: Serguei.Kaniovski at wifo.ac.at
> >
> > http://www.wifo.ac.at/Serguei.Kaniovski
> >
> > ______________________________________________
> > R-help at 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
> > and provide commented, minimal, self-contained, reproducible code.
>
> ---------------------------------------------------------------------------
>--- Notice:  This e-mail message, together with any
> attachments,...{{dropped}}

-- 
Adrian DUSA
Arhiva Romana de Date Sociale
Bd. Schitu Magureanu nr.1
050025 Bucuresti sectorul 5
Romania
Tel./Fax: +40 21 3126618 \
          +40 21 3120210 / int.101




More information about the R-help mailing list