[R] omit empty cells in crosstab?

Bernardo Rangel Tura tura at centroin.com.br
Sun Apr 26 11:49:40 CEST 2009


On Fri, 2009-04-24 at 13:12 -0700, sjaffe wrote:
> small example:
> 
> a<-c(1.1, 2.1, 9.1)
> b<-cut(a,0:10)
> c<-data.frame(b,b)
> d<-table(c)
> dim(d) 
> ##result: c(10, 10)
> 
> But only 9 of the 100 cells are non-zero.
> If there were 10 columns, the table have 10 dimensions each of length 10, so
> have 10^10 elements, too much even to fit in memory

Hi Steve

In your only 3 cells > 0
> d
        b.1
b        (0,1] (1,2] (2,3] (3,4] (4,5] (5,6] (6,7] (7,8] (8,9] (9,10]
  (0,1]      0     0     0     0     0     0     0     0     0      0
  (1,2]      0     1     0     0     0     0     0     0     0      0
  (2,3]      0     0     1     0     0     0     0     0     0      0
  (3,4]      0     0     0     0     0     0     0     0     0      0
  (4,5]      0     0     0     0     0     0     0     0     0      0
  (5,6]      0     0     0     0     0     0     0     0     0      0
  (6,7]      0     0     0     0     0     0     0     0     0      0
  (7,8]      0     0     0     0     0     0     0     0     0      0
  (8,9]      0     0     0     0     0     0     0     0     0      0
  (9,10]     0     0     0     0     0     0     0     0     0      1

If you desire use simple code to find only cell>0 use this

 table(interaction(c,drop=T))

  (1,2].(1,2]   (2,3].(2,3] (9,10].(9,10] 
            1             1             1 


-- 
Bernardo Rangel Tura, M.D,MPH,Ph.D
National Institute of Cardiology
Brazil




More information about the R-help mailing list