R
order(X, na.last=TRUE, decreasing=FALSE)
返回值: X排好序的下标向量
na.last 控制空值NA排在最前还是最后,默认最后
desceasing 控制升序还是降序排列
例子:
#vectorX <- 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 7table_1 <- data.frame(x=X, y=Y)table_1 x y1 7 502 4 803 5 304 3 705 8 206 1 107 9 408 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 y6 1 108 3 904 3 702 4 803 5 301 7 505 8 207 9 40 table_1[order(-table_1$x, table_1$y), ] #X desc, Y asc x y7 9 405 8 201 7 503 5 302 4 804 3 708 3 906 1 10 table_1[order(-table_1$x, -table_1$y), ] #X desc, Y desc x y7 9 405 8 201 7 503 5 302 4 808 3 904 3 706 1 10