[R] basic question
Christoph Lehmann
christoph.lehmann at gmx.ch
Thu Apr 21 18:44:35 CEST 2005
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 at 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
>
>
More information about the R-help
mailing list