Re: [R] basic question

From: Christoph Lehmann <christoph.lehmann_at_gmx.ch>
Date: Fri 22 Apr 2005 - 02:44:35 EST

sapply(split(test, test$year), function(x) list(x.s = sum(x$x), y.s = sum(x$y), z.s = sum(x$z)))

or for one variable only

aggregate(test$x, list(id = test$year), sum)

cheers
christoph

jose silva wrote:
> I know this question is very simple, but I am not figure it out
>
>
>
>
> I have the data frame:

>
>
>
>
>
>
>
>
>
> test<- data.frame(year=c(2000,2000,2001,2001),x=c(54,41,90,15), y=c(29,2,92,22), z=c(26,68,46,51))
>
>
>
>
> test
>
>
>
>
> year x y z
>
>
>
>
> 1 2000 54 29 26
>
>
>
>
> 2 2000 41 2 68
>
>
>
>
> 3 2001 90 92 46
>
>
>
>
> 4 2001 15 22 51
>
>
>
>
>
>
>
>
>
> I want to sum the vectors x, y and z within each year (2000 and 2001) to obtain this:

>
>
>
>
>
>
>
>
>
> year x y z
>
>
>
>
> 1 2000 95 31 94
>
>
>
>
> 2 2001 105 114 97
>
>
>
>
>
>
>
>
>
> I tried tapply but did not work (or probably I do it wrong)

>
>
>
>
>
>
>
>
>
> Any suggestions?
>
>
>
>
>
>
>
>
>
> silva
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
>



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Fri Apr 22 02:05:49 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:31:21 EST