Consider the following data.frame, d. d <- data.frame(c("a","b","c","d","b","a"), c(1,4,85,3,4,1), c(7,6,2,4,15,33)) names(d) <- c("foo", "bar", "baz") To get the colsum of d[[2]] for the rows that have d$foo == "a" I know I can use colSums(subset(d, d[[1]] == "a", select = 2)) But what is needed to get a list of colSums for d[[2]] for each factor of d$foo ? Can it be done without a for loop? -- Hans Ekbrand (http://sociologi.cjb.net) <hans at sociologi.cjb.net> GPG Fingerprint: 1408 C8D5 1E7D 4C9C C27E 014F 7C2C 872A 7050 614E -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: Digital signature Url : https://stat.ethz.ch/pipermail/r-help/attachments/20080318/7adf898e/attachment.bin
Is this what you want?> lapply(split(d, d$foo), function(x) colSums(x[,-1]))$a bar baz 2 40 $b bar baz 8 21 $c bar baz 85 2 $d bar baz 3 4>On Tue, Mar 18, 2008 at 5:41 AM, Hans Ekbrand <hans at sociologi.cjb.net> wrote:> Consider the following data.frame, d. > > d <- data.frame(c("a","b","c","d","b","a"), c(1,4,85,3,4,1), c(7,6,2,4,15,33)) > names(d) <- c("foo", "bar", "baz") > > To get the colsum of d[[2]] for the rows that have d$foo == "a" I know > I can use > > colSums(subset(d, d[[1]] == "a", select = 2)) > > But what is needed to get a list of colSums for d[[2]] for each factor > of d$foo ? Can it be done without a for loop? > > -- > Hans Ekbrand (http://sociologi.cjb.net) <hans at sociologi.cjb.net> > GPG Fingerprint: 1408 C8D5 1E7D 4C9C C27E 014F 7C2C 872A 7050 614E > > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.4.6 (GNU/Linux) > > iD8DBQFH35xlfCyHKnBQYU4RAqItAKC4Ksiq/ZP0mK98+Bp9nCeVt4m95QCfWALV > b8WM/M+ITG502zi0McHnV30> =/dm6 > -----END PGP SIGNATURE----- > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?
try: aggregate(d[,2:3], by=list(d$foo), FUN=sum) cheers, Albert Am Dienstag, den 18.03.2008, 11:41 +0100 schrieb Hans Ekbrand:> Consider the following data.frame, d. > > d <- data.frame(c("a","b","c","d","b","a"), c(1,4,85,3,4,1), c(7,6,2,4,15,33)) > names(d) <- c("foo", "bar", "baz") > > To get the colsum of d[[2]] for the rows that have d$foo == "a" I know > I can use > > colSums(subset(d, d[[1]] == "a", select = 2)) > > But what is needed to get a list of colSums for d[[2]] for each factor > of d$foo ? Can it be done without a for loop? > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
On Tue, Mar 18, 2008 at 05:57:59AM -0500, jim holtman wrote:> Is this what you want? > > > lapply(split(d, d$foo), function(x) colSums(x[,-1]))Yes! Thank you! the *apply functions seem very powerful, thanks again for giving me a hint on how to use them. -- Hans Ekbrand (http://sociologi.cjb.net) <hans at sociologi.cjb.net> Q. What is that strange attachment in this mail? A. My digital signature, see www.gnupg.org for info on how you could use it to ensure that this mail is from me and has not been altered on the way to you. -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: Digital signature Url : https://stat.ethz.ch/pipermail/r-help/attachments/20080318/9444e116/attachment.bin
See ?rowsum. The other two suggestions are both shown in the Example section as well (with comment). Andy From: Hans Ekbrand> > * PGP Signed by an unknown key: 03/18/2008 at 06:22:03 AM > On Tue, Mar 18, 2008 at 05:57:59AM -0500, jim holtman wrote: > > Is this what you want? > > > > > lapply(split(d, d$foo), function(x) colSums(x[,-1])) > > Yes! Thank you! the *apply functions seem very powerful, thanks again > for giving me a hint on how to use them. > > -- > Hans Ekbrand (http://sociologi.cjb.net) <hans at sociologi.cjb.net> > Q. What is that strange attachment in this mail? > A. My digital signature, see www.gnupg.org for info on how you could > use it to ensure that this mail is from me and has not been > altered on the way to you. > * Unknown Key > * 0x7050614E (L) > >------------------------------------------------------------------------------ Notice: This e-mail message, together with any attachme...{{dropped:15}}
On Tue, Mar 18, 2008 at 12:05:23PM +0100, Albert Greinoecker wrote:> try: > aggregate(d[,2:3], by=list(d$foo), FUN=sum)Great! Now I can get a data.frame as well as a list, thanks! -- Hans Ekbrand (http://sociologi.cjb.net) <hans at sociologi.cjb.net> Signature generated by Signify v1.14. For this and more, visit http://www.debian.org/ -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: Digital signature Url : https://stat.ethz.ch/pipermail/r-help/attachments/20080318/e5eca97d/attachment.bin