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..
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With