订单如何运作?

时间:2013-06-18 03:45:24

标签: r sorting

这是一个非常令人尴尬的问题,但我无法理解以下结果:

> order(c(3,1,2))
[1] 2 3 1

那么,它是说有序序列是2, 3, 1?怎么样?

1 个答案:

答案 0 :(得分:4)

> a <- c(30,10,20)     # sample data
> sort(a)              # sort returns your vector sorted
[1] 10 20 33
> order(a)             # order returns the *indices* in the sorted vector
[1] 2 3 1
> a[order(a)]          # so if you select your numbers with those indices
[1] 10 20 30           # you get your vector sorted