Reputation: 137
I have a large data set like this:
df <- data.frame(group = c(rep(1, 3), rep(5, 3)), member = c(30, 10, 22, 21, 44, 15))
group member
1 30
1 10
1 22
5 21
5 44
5 15
...
I want to order member within each group. The expected output should look like this:
group member
1 10
1 22
1 30
5 15
5 21
5 44
...
Does anyone have idea about realizing this?
Upvotes: 1
Views: 63
Reputation: 4284
You can use the "with" function : first argument is your dataframe and second one is what you want to do with it : order by variable "group" then by variable "member".
df[with(df,order(group,member)),]
Upvotes: 1
Reputation: 61214
Try using ddply
from plyr package
> library(plyr)
> ddply(df, .(group), mutate, member=sort(member))
group member
1 1 10
2 1 22
3 1 30
4 5 15
5 5 21
6 5 44
Upvotes: 0