s-news
[Top] [All Lists]

using split() and lapply instead of tapply

To: s-news@lists.biostat.wustl.edu
Subject: using split() and lapply instead of tapply
From: Martyn Colins <martyncollins10@yahoo.com>
Date: Wed, 2 Aug 2006 05:34:15 -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=twK6G1hLIm+Bd6u2aZt2zgpMxt8QC3uo+gW15o276SgS3gsdyNmIIO7qfyAoGYXz0NYMrFwu/q5r2+3ddEnyCV9bZBKm7RRXOiTyGsxUK2BRZYpKOl0hISCHXQh3SfK/0RYgz1cp3lcWhRhwMmHrXwsBlNOeVVO0od+EhcALDa8= ;
 
Does anyone know how to use the commands split() and lapply() to do the following: suppose 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"))
this is the output that i'm after
> tapply(w$y1, list(w$y2, w$y3), sum)
  level1 level2
A      1      6
B      7      2
C      3      8
D      9      4
E      5     10
 
I would like to do this using the split and lapply commands as these are more efficient than tapply.
I can split w on one variable
w2 <- split(w, w$y2)
but how do I split on both variables to produce the equivalent output?
 
Thanks in advance for any help.
 
Regards,
Martyn


Groups are talking. We´re listening. Check out the handy changes to Yahoo! Groups.
<Prev in Thread] Current Thread [Next in Thread>
  • using split() and lapply instead of tapply, Martyn Colins <=