s-news
[Top] [All Lists]

Re: tapply

To: Manoel Pacheco <mpacheco@glec.com>
Subject: Re: tapply
From: Sundar Dorai-Raj <sundar.dorai-raj@pdf.com>
Date: Mon, 02 Apr 2007 14:04:28 -0700
Cc: s-news@lists.biostat.wustl.edu
In-reply-to: <002101c77569$bc7b94e0$0401a8c0@Manolo>
Organization: PDF Solutions, Inc.
References: <002101c77569$bc7b94e0$0401a8c0@Manolo>
User-agent: Thunderbird 1.5.0.10 (Windows/20070221)


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

<Prev in Thread] Current Thread [Next in Thread>
  • tapply, Manoel Pacheco
    • Re: tapply, Sundar Dorai-Raj <=