I'm writting my first program in R and as a newbie I'm having some troubles, hope you can help me.
I've got a data frame like this:
> v1<-c(1,1,2,3,3,3,4)
> v2<-c(13,5,15,1,2,7,4)
> v3<-c(0,3,6,13,8,23,5)
> v4<-c(26,25,11,2,8,1,0)
> datos<-data.frame(v1,v2,v3,v4)
> names(datos)<-c("Position","a1","a2","a3")
> datos
posicion a1 a2 a3
1 1 13 0 26
2 1 5 3 25
3 2 15 6 11
4 3 1 13 2
5 3 2 8 8
6 3 7 23 1
7 4 4 5 0
What I need is to sum the data in a1
, a2
and a3
(in my real case from a1
to a51
) grouped by Position
. I'm trying with the function aggregate()
but it only works for means, not for sums and I don't know why.
Thanks in advance