如何根据元素的手动位置对 R 中的向量进行排序?


要根据元素的手动位置对向量进行排序,我们可以将 order 函数与 factor 函数结合使用。factor 函数将通过将级别定义为向量元素来帮助我们将向量元素按我们希望的顺序排列,而 order 函数将对它们进行排序。查看以下示例以了解其工作原理。

示例 1

实时演示

> x1<-sample(0:4,200,replace=T)
> x1

输出

[1] 0 1 0 0 1 2 1 2 3 1 2 2 2 4 3 1 4 1 0 1 1 3 3 0 0 4 4 2 4 2 4 2 0 4 0 1 1
[38] 4 2 0 1 0 3 0 4 3 2 0 0 3 2 0 1 0 0 0 4 2 2 4 2 0 4 3 3 1 3 1 2 3 0 2 2 0
[75] 1 1 4 4 4 0 4 0 3 4 0 3 1 4 2 2 3 0 2 4 3 1 2 1 1 3 1 3 4 4 3 1 1 4 4 1 0
[112] 2 2 4 0 0 2 2 1 2 4 4 0 4 1 2 0 3 0 0 4 1 2 4 0 1 1 0 4 1 3 2 0 4 4 2 4 1
[149] 3 4 4 3 2 1 2 2 0 3 4 1 0 1 4 4 0 4 0 2 3 4 1 4 3 2 1 2 4 0 1 3 0 4 2 4 4
[186] 4 3 2 3 4 1 1 0 0 4 0 4 4 0 2

示例

> x1[order(factor(x1,levels=c(3,1,0,4,2)))]

输出

[1] 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 1 1 1 1 1 1 1 1
[38] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0
[75] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[112] 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
[149] 4 4 4 4 4 4 4 4 4 4 4 4 4 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
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 1 4 3
[151] 2 6 1 6 2 5 5 7 8 10 5 5 1 9 10 10 3 6 10 5 3 4 2 10 9
[176] 2 7 4 3 3 9 3 5 1 3 8 1 5 7 8 9 8 3 5 2 1 7 6 2 9
[201] 9 5 6 7 10 7 3 6 5 2 5 8 1 3 9 1 5 10 7 3 9 3 3 7 3
[226] 2 7 3 8 1 6 2 8 3 3 9 8 10 3 10 10 8 8 2 6 9 7 3 6 6
[251] 10 9 4 2 9 8 10 7 10 2 1 8 10 10 7 2 10 8 2 5 10 4 10 8 9
[276] 1 9 4 10 4 1 2 7 1 9 2 5 3 5 3 6 9 9 6 8 7 10 5 6 9

示例

> x4[order(factor(x4,levels=c(2,4,6,8,10,1,3,5,7,9)))]

输出

[1] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 4 4 4 4 4
[26] 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 6 6 6 6
[51] 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6
[76] 6 6 6 6 6 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8
[101] 8 8 8 8 8 8 8 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10
[126] 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 1 1 1 1 1 1
[151] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[176] 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
[201] 3 3 3 3 3 3 3 3 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
[226] 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 7 7 7 7 7 7 7 7 7
[251] 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 9 9 9 9 9 9 9 9
[276] 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9

示例 5

实时演示

> x5<-sample(21:25,200,replace=T)
> x5

输出

[1] 25 22 25 24 24 25 24 24 22 22 25 25 24 21 21 24 24 24 25 22 24 22 25 22 24
[26] 21 24 24 21 21 25 22 23 21 21 22 23 22 22 25 23 25 22 21 22 25 24 25 21 24
[51] 23 21 22 22 24 24 24 24 25 24 23 21 25 24 22 23 23 24 25 21 23 23 21 25 22
[76] 23 21 25 23 21 23 21 25 24 22 24 22 22 21 21 21 24 22 25 23 22 23 24 24 22
[101] 23 21 24 25 24 22 25 25 22 22 22 25 25 22 24 25 22 22 24 23 23 21 22 25 25
[126] 21 24 23 25 25 23 22 21 21 22 24 24 24 25 24 24 22 23 22 21 23 21 24 25 23
[151] 25 22 21 25 25 23 23 23 24 21 24 22 21 22 24 25 22 24 23 22 25 24 23 23 24
[176] 25 24 25 23 23 21 22 22 24 21 22 24 21 22 22 24 24 25 24 21 22 25 23 23 22

示例

> x5[order(factor(x5,levels=c(25,24,21,22,23)))]

输出

[1] 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25
[26] 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 24 24 24 24 24 24 24 24 24 24
[51] 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24
[76] 24 24 24 24 24 24 24 24 24 24 24 24 24 24 21 21 21 21 21 21 21 21 21 21 21
[101] 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 22 22 22
[126] 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22
[151] 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 23 23 23 23 23 23 23
[176] 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23

更新时间:2021-03-05

541 次浏览

提升你的 职业生涯

完成课程即可获得认证

开始
广告