> #vector
> X <- c(7,4,5,2,8,1,9,3) > order(X)
[1] 6 4 8 2 3 1 5 7
> X[order(X)]
[1] 1 2 3 4 5 7 8 9
> X[order(X, decreasing=TRUE)]
[1] 9 8 7 5 4 3 2 1
> order(-X) # '-' equals decreasing=TRUE
[1] 7 5 1 3 2 4 8 6
> #vector
> #dataframe.
> X <- c(7,4,5,3,8,1,9,3)
> Y <- c(50, 80, 30, 70, 20, 10, 40, 90)
> order(X, Y) #only print X's order, no Y's
[1] 6 4 8 2 3 1 5 7
> table_1 <- data.frame(x=X, y=Y)
> table_1
x y
1 7 50
2 4 80
3 5 30
4 3 70
5 8 20
6 1 10
7 9 40
8 3 90
> order(table_1$x, table_1$y) #X asc, Y asc, print the row number's sequence
[1] 6 4 8 2 3 1 5 7
> table_1[order(table_1$x, -table_1$y),] #X asc, Y desc
x y
6 1 10
8 3 90
4 3 70
2 4 80
3 5 30
1 7 50
5 8 20
7 9 40
> table_1[order(-table_1$x, table_1$y), ] #X desc, Y asc
x y
7 9 40
5 8 20
1 7 50
3 5 30
2 4 80
4 3 70
8 3 90
6 1 10
> table_1[order(-table_1$x, -table_1$y), ] #X desc, Y asc
x y 7 9 40 5 8 20 1 7 50 3 5 30 2 4 80 8 3 90 4 3 70 6 1 10
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/26935210/viewspace-722825/ ,如需转载,请注明出处,否则将追究法律责任。