I have a data frame
dat <- data.frame(fac=c("a","a","a","b","b","b","c","c","c"),
r=c(0.6, 1.5, 5, 0.1, 1.8, 10, 0.9, 1.2, 1.9) )
and I wish to extract the length of a subset of each factor level.
I know that
tapply(dat$r, dat$fac, length)
a b c
3 3 3
gives me the length of each factor level, but I am interested in
length(dat[dat$fac == "a" & dat$r >= 0.5 & dat$r <= 2, 2]
length(dat[dat$fac == "b" & dat$r >= 0.5 & dat$r <= 2, 2]
length(dat[dat$fac == "c" & dat$r >= 0.5 & dat$r <= 2, 2]
so that the output of the tapply would be
a b c
2 1 3
I tried without success to incorporate the subset criteria into tapply.
I would appreciate suggestions to solve this problem. Thanks,
Manolo
|