David Gerrard
David Gerrard

Reputation: 161

Sorting an aggregate output by a specific column in R

I am trying to sort the output of an aggregate function that is passed multiple functions, as I want to show the means and counts of each pairing.

I am calling as follows (using builtin dataset Loblolly as an example):

> LobHeigh <- aggregate(Loblolly$height, by=list(Loblolly$Seed,     
Loblolly$age),FUN=function(x) c(mn=mean(x), n=length(x)))
> head(LobHeigh)

  Group.1 Group.2 x.mn  x.n
1     329       3 3.93 1.00
2     327       3 4.12 1.00
3     325       3 4.38 1.00
4     307       3 3.91 1.00
5     331       3 3.46 1.00
6     311       3 3.88 1.00

I would like this output to look as follows (# of decimal places is fine either way):

Group.1 Group.2     x.mn    x.n
    325       3     4.38      1
    327       3     4.12      1
    329       3     3.93      1
    307       3     3.91      1
    311       3     3.88      1
    331       3     3.46      1

I have attempted to write sort into the aggregate call as follows, but this hasn't worked:

LobHeigh <- aggregate(Loblolly$height, by=list(Loblolly$Seed,     
Loblolly$age),FUN=function(x) c(mn=sort(mean(x)), n=length(x)))

I have also attempted to sort LobHeigh after the function call as follows, as I know this works if I only aggregate on the mean by itself:

> LobHeigh <- aggregate(Loblolly$height, by=list(Loblolly$Seed,     
Loblolly$age),FUN=function(x) c(mn=mean(x), n=length(x)))
> LobHeigh[order(LobHeigh$x),]

I know that class(LobHeigh$x) shows that x is a matrix, however I cannot work out how to access x.mn to use it to sort by.

Is there a way to do this?

Upvotes: 1

Views: 4614

Answers (1)

Josh
Josh

Reputation: 1278

You can access the column by double indexing, LobHeigh[,3][,1].

LobHeigh[sort(LobHeigh[,3][,1], TRUE),]

That said, you're probably better off making it a more traditional matrix.

cbind(LobHeigh[,1:2], LobHeigh[,3])

and working with it from there.

Upvotes: 2

Related Questions