Thursday, July 11, 2013

Group columns by sum/frequency - R

I recently came across a need to group by data by count/frequency and wanted to go with a method that doesn't use any external libraries. I chose aggregate. Run ?aggregate() and find out more what this can do for you.


Using aggregate:
refugees <- data.frame(Names=factor(c("Okello", "Odongo", "Kiagiri",  "Wachira", "Atata", "Okello")),  actionType=c(101,103,101,102,104,200,200))
Group by length/frequency.
aggregate(refugees$actionType, by=list(refugees$Names), FUN=length)
Group.1 x
1   Atata 1
2 Kiagiri 1
3  Odongo 1
4  Okello 3
5 Wachira 1



Group by sum.


aggregate(refugees$actionType, by=list(refugees$Names), FUN=sum)


Group.1   x
1   Atata 200
2 Kiagiri 101
3  Odongo 103
4  Okello 403
5 Wachira 104

No comments:

Post a Comment

Add any comments if it helped :)