s-news
[Top] [All Lists]

using tapply to filter a variable

To: s-news@lists.biostat.wustl.edu
Subject: using tapply to filter a variable
From: Martyn Colins <martyncollins10@yahoo.com>
Date: Tue, 1 Aug 2006 06:17:07 -0700 (PDT)
Domainkey-signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=Message-ID:Received:Date:From:Subject:To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=3eFjDZSpIEWGj8hmufB5X/FW4VTkTnjCEu1UySW5AYvNWM7LYvemoJlr/xNF3xM/tU/LItSGbynZrB3KIgWQaE71gOB6NUlZw1knL72J3+OdFzSyQtpEgjpVeiaULD9zHLcIMeUBhVGF9lEWkNTpndsVlpZLQz9XBARrqDgPvz4= ;
 
I have a question related to the tapply function. If I have the following data.frame:
 
w <- data.frame(y1 =1:10, y2=as.factor(rep(LETTERS[1:5],2)), y3=as.factor(rep(c("level1", "level2"),5)), y4=c(rep(c("sub1", "sub2", "sub3"), 3), "sub3"))
 
and I want to sum w$y1 for w$y2 and w$y3 when w$y4 is equal to "sub1" - I tried this
tapply(w$y1, list(w$y2, w$y3, w$y4=="sub1"), sum)
 
However, I get 2 tables (FALSE and TRUE) like this
> tapply(w$y1, list(w$y2, w$y3, w$y4 == "sub1"), sum)
, , FALSE
level1 level2
A NA 6
B NA 2
C 3 8
D 9 NA
E 5 10
, , TRUE
level1 level2
A 1 NA
B 7 NA
C NA NA
D NA 4
E NA NA
 
How do I get the table for "sub1"  without performing the "sum" operation for when w$y4 is not equal to "sub1", i.e. I want the TRUE table without calculating the FALSE table as I'm performing a computation intensive operation?
 
Thanks in advance for any help.
 
Martyn 


Yahoo! Music Unlimited - Access over 1 million songs. Try it free.
<Prev in Thread] Current Thread [Next in Thread>
  • using tapply to filter a variable, Martyn Colins <=