[R] table problems

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Thu Jun 13 17:13:00 CEST 2002


ripley at stats.ox.ac.uk writes:

> On Thu, 13 Jun 2002, David Brahm wrote:
> 
> > Robin Hankin <r.hankin at auckland.ac.nz> has a vector of trees visited:
> >   R> c <- c(3,14,17,22,26,26,17,40,43,25,46,46)
> > and wants a count of visits for trees 1:50.  table(c) is inadequate because it
> > omits the zero cases.
> >
> > Brian D. Ripley <ripley at stats.ox.ac.uk> suggested:
> > > Try tabulate(c), which goes to 46.  Or, better,
> > > tab <- rep(0,50)
> > > names(tab) <- 1:50
> > > tab[names(table(c))] <- table(c)
> >
> > But tabulate has a second argument (nbins) which does what you want in 1 line:
> >   R> tabulate(c, 50)
> 
> Yes if you want 1:50.  Not for other label sets, which is why I did it
> the more general way.  I did mention tabulate: should have givne more
> details, I guess.

My standard device would be

table(factor(c,levels=1:50))

-- 
   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
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list