3

Possible Duplicate:
How to sort a dataframe by column(s) in R

I have a dataset that looks like this:

x       y     z
1.      1     0.2
1.1     1     1.5
1.2     1     3.
1.      2     8.1
1.1     2     1.0
1.2     2     0.6

What I would like is organise the dataset first as a function of x in increasing order then as a function of y such that

x       y      z 
1.      1      0.2
1.      2      8.1
1.1     1      1.5
1.1     2      1.
1.2     1      3.
1.2     2      0.6

I know that apply, mapply, tapply, etc functions reorganise datasets but I must admit that I don't really understand the differences between them nor do I really understand how to apply which and when.

Thank you for your suggestions.

Community
  • 1
  • 1
SnowFrog
  • 1,162
  • 4
  • 19
  • 38
  • Related questions: http://stackoverflow.com/q/6769703/602276 and http://stackoverflow.com/q/5602525/602276 – Andrie Aug 22 '11 at 09:23
  • Comparison of various sort methods: http://stackoverflow.com/questions/1296646/how-to-sort-a-dataframe-by-columns-in-r/6871968#6871968 – Ari B. Friedman Aug 22 '11 at 09:32

2 Answers2

8

You can order your data using the order function. There is no need for any apply family function.

Assuming your data is in a data.frame called df:

df[order(df$x, df$y), ]
    x y   z
1 1.0 1 0.2
4 1.0 2 8.1
2 1.1 1 1.5
5 1.1 2 1.0
3 1.2 1 3.0
6 1.2 2 0.6

See ?order for more help.


On a side note: reshaping in general refers to changing the shape of a data.frame, e.g. converting it from wide to tall format. This is not what is required here.

Andrie
  • 176,377
  • 47
  • 447
  • 496
  • Very simple and efficient way to re-order my dataset. Thank you for the quick answers and suggestions for me to understand the differences between order(), reshape() etc... – SnowFrog Aug 22 '11 at 10:35
4

You can also use the arrange() function in plyr for this. Wrap the variables in desc() that you want to sort the other direction.

> library(plyr)
> dat <- head(ChickWeight)
> arrange(dat,weight,Time)
  weight Time Chick Diet
1     42    0     1    1
2     51    2     1    1
3     59    4     1    1
4     64    6     1    1
5     76    8     1    1
6     93   10     1    1

This is the fastest way to do this that's still readable, if speed matters in your application. Benchmarks here: How to sort a dataframe by column(s)?

Community
  • 1
  • 1
Ari B. Friedman
  • 71,271
  • 35
  • 175
  • 235