In R, order()
returns the row index vector. In your example:
> order(data$x) [1] 3 1 2
Which you can interpret as "the lowest value on line 3, the second lowest on line 1", etc. It is important to note that it does not in any way alter the data structure. To get a data frame sorted by x using order()
, you can simply use:
> data[order(data$x),] xy 3 1 3 1 2 1 2 3 2
Gala source share