Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Aggregate function in R

Tags:

r

aggregation

can you help me please how to properly use aggregate function in R? I have data like this:

SPORT   FLOWS
[1,] "Other" "1"  
[2,] "Other" "1"  
[3,] "Other" "1"  
[4,] "Other" "1"  
[5,] "Other2" "1"  
[6,] "Other2" "1"

And I need to get this:

SPORT   FLOWS
[1,] "Other" "4"
[2,] "Other2" "2"

I found, that it can be done with aggregate function, but it doesn't work..

Thank you guys.. I have marked answer which worked for me..

like image 435
Jan Jůna Avatar asked Dec 16 '13 20:12

Jan Jůna


People also ask

How does aggregate function work in R?

In R, you can use the aggregate function to compute summary statistics for subsets of the data. This function is very similar to the tapply function, but you can also input a formula or a time series object and in addition, the output is of class data.


1 Answers

aggregate(FLOWS ~ SPORT, dat, function(x) sum(as.numeric(x)))

where dat is the name of your matrix.

Here, the function is.numeric is necessary to transform the second column into numbers.

like image 150
Sven Hohenstein Avatar answered Sep 21 '22 11:09

Sven Hohenstein