[R] Working with tables with missing levels

Andre Nathan andre at digirati.com.br
Mon Jul 27 21:56:38 CEST 2009


On Mon, 2009-07-27 at 16:34 -0300, Henrique Dallazuanna wrote:
> Try this:
> 
> t1 <- prop.table(table(factor(c(0,0,2,4,4), levels = 0:4)))
> t2 <- prop.table(table(factor(c(0,2,2,2,3), levels = 0:4)))

Is there a way to do this given an already existing table? The problem
is that I actually build the distributions as I read data from files,
something like

  distr <- NULL
  for (file in files) {
    x <- as.matrix(read.table(file))
    t <- c(distr, table(x))
    distr <- tapply(t, names(t), sum)
  }
  distr <- prop.table(distr)

So I only know the maximum level after the distributions are created.

Thanks,
Andre




More information about the R-help mailing list