[R] tapply and names

Göran Broström gb at tal.stat.umu.se
Tue Jan 25 15:55:00 CET 2005


I have a data frame containing children, with variables 'year' = birth
year, and 'm.id' = mother's id number. Let's assume that all the births of
each mother is represented in the data frame. 

Now I want to create a subset of this data frame containing all children,
whose mother's first birth was in the year 1816 or later. This seems to
work: 

    mid <- tapply(dat$year, dat$m.id, min)
    mid <- as.numeric(names(mid)[mid >= 1816])
    dat <- dat[dat$m.id %in% mid, ]

but I'm worried about the second line, because the output from 'tapply'
isn't documented to have a 'dimnames' attribute (although it has one, at
least in R-2.1.0, 2005-01-19). Another aspect is that this code relies on
m.id being numeric; I would have to change it if the type of m.id changes
to, eg, character.

So, question: Is there a better way of doing this?
-- 
 Göran Broström                    tel: +46 90 786 5223
 Department of Statistics          fax: +46 90 786 6614
 Umeå University                   http://www.stat.umu.se/egna/gb/
 SE-90187 Umeå, Sweden             e-mail: gb at stat.umu.se




More information about the R-help mailing list