Manoel Pacheco said the following on 4/2/2007 1:58 PM:
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
--------------------------------------------------------------------
This message was distributed by s-news@lists.biostat.wustl.edu. To
unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
the BODY of the message: unsubscribe s-news
You'll have to write your own function
tapply(dat$r, dat$fac, function(x) sum(x >= 0.5 & x <= 2))
HTH,
--sundar
|