[R] Count cases by indicator

Liaw, Andy andy_liaw at merck.com
Mon Dec 4 14:24:01 CET 2006


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}}




More information about the R-help mailing list