Re: [R] grouped colSums without for loops?

From: Albert Greinoecker <albert.greinoecker_at_uibk.ac.at>
Date: Tue, 18 Mar 2008 12:05:23 +0100

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.



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. Received on Tue 18 Mar 2008 - 11:12:19 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 18 Mar 2008 - 23:30:24 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive