order函数

2024-08-26 23:40:31
英语时代
英语时代认证

英语时代为您分享以下优质知识

1、order函数用于返回向量大小顺序的秩。

测试:

a <- c(3,5,2,0)

order(a)

代码效果:

b <- c(200,600,800,400,100)

order(b)

代码效果:

可见 order函数默认用于返回向量从下到大排序在原始向量中的位次(秩)。

2、加参数decreasing = T,降序排列

测试:

a <- c(3,5,2,0)

order(a,decreasing = T)

代码效果:

b <- c(200,600,800,400,100)

order(b,decreasing = T)

代码效果:

3、利用以上order函数的性质可以实现对数据框的排序

创建测试数据框:

a <- c(3,7,5,2,9)

b <- c(9,3,7,8,1)

c <- c(3,7,8,2,5)

d <- data.frame(a,b,c)

d

代码效果:

利用order函数对数据框的任意一列进行排序: