[R] table function

Peter Dalgaard P.Dalgaard at biostat.ku.dk
Mon Jul 16 12:03:13 CEST 2007

sigalit mangut-leiba wrote:
> Hello all, 
> I want to use the "table" function, but for every id I have a different no.
> of rows (obs.).
> If I write:
> table(x$class, x$infec)
> I don't get the right frequencies because I should count every id once, if
> id 1 has 20 observations It should count as one.
> can I use unique func. here?
> Hope it's clear.
Almost. I assume that class and infect are constant over id?  (If people
change infection status during the trial, you have a more complex problem).

You could then use unique() like this

with(unique(x[c("id", "class", "infec")] , table(class, infec)),

but I'd prefer using duplicated() as in

with(subset(x, !duplicated(id)), table(class, infec))

(notice that the latter tabulates the first record for each id, whereas
the former will count ids multiple times if the change class or infec).

   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907

More information about the R-help mailing list