4-6 R语言函数 排序

#sort:对向量进行排序;返回排好序的内容
#order:返回排好序的内容的下标/多个排序标准

> x <- data.frame(v1=1:5,v2=c(10,7,9,6,8),v3=11:15,v4=c(1,1,2,2,1))

> sort(x$v2)
[1]  6  7  8  9 10

> sort(x$v2,decreasing = TRUE)
[1] 10  9  8  7  6

> order(x$v2)
[1] 4 2 5 3 1

> x[order(x$v2),]
  v1 v2 v3 v4
4  4  6 14  2
2  2  7 12  1
5  5  8 15  1
3  3  9 13  2
1  1 10 11  1

> x[order(x$v4,x$v2,decreasing = TRUE),]
  v1 v2 v3 v4
3  3  9 13  2
4  4  6 14  2
1  1 10 11  1
5  5  8 15  1
2  2  7 12  1

猜你喜欢

转载自www.cnblogs.com/hankleo/p/9942345.html
4-6