[R] Frequency table

Peter Dalgaard p.dalgaard at biostat.ku.dk
Wed Mar 17 20:02:55 CET 2004


Prof Brian Ripley <ripley at stats.ox.ac.uk> writes:

> > tt<-table(zz%/%10)
> > n <- names(tt)
> > names(tt) <- paste(n,0,"-",n,9,sep="")
> > tt
> > data.frame(count=c(tt))
> > 
> > Beware that empty groups are silently zapped, though.
> 
> FWIW, table(factor(zz%/%10, levels=0:9))  avoids that

I knew, but then you'd basically be back to using cut. Your fix will
lose if there are any 107-year olds, by the way. It will also include
a number of empty groups below the range of data, which might not be
what you want.

Here's a first stab at a more general solution:

sz <- 10 
g <- zz %/% sz
lv <- min(g):max(g)
lb <- paste(lv * sz, "-", lv * sz + sz - 1, sep="")
f <- factor(g,levels=lv,labels=lb)
(tt <- table(f))
data.frame(count=c(tt))


-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list