I'm trying to use the aggregate
function with cbind
, but I must be missing something.
I've seen in Using Aggregate for Multiple Aggregations that I can simply define which column I want to be fixed and which I'd like to add, but I just can't get the result I expected.
I have:
x <- data.frame(alfa = 1:9, beta = rep(1:3, 3))
alfa beta
1 1 1
2 2 2
3 3 3
4 4 1
5 5 2
6 6 3
7 7 1
8 8 2
9 9 3
And I want to retrieve the mean of the entries aggregated by the ones in column beta. For that I've tried:
aggregate(cbind(alfa) ~ beta, data = x, FUN = function(x) c(gama = mean(x)) )
That gives me:
beta alfa
1 1 4
2 2 5
3 3 6
Shouldn't the result be something like:
alfa beta gama
1 1 1 4
2 2 2 5
3 3 3 6
How do I force the addition of column gama? Additionally, would someone clarify the basis of the cbind()
function? I've been struggling to understand it. Regards!