在 R 编程中计算向量值的秩 – rank()函数
R 语言中的rank()
函数用于返回向量值的样本等级。等值和缺失值有多种处理方式。
Syntax: rank(x, na.last)
Parameters:
x: numeric, complex, character, and logical vector
na.last: Boolean Value to remove NAs
示例 1:
# R program to print rank of a vector values
# Creating a vector
x <- c(2, 5, 3, 6, -4, NA, 7, Inf, 5)
# Calling rank() function
rank(x)
输出:
[1] 2.0 4.5 3.0 6.0 1.0 9.0 7.0 8.0 4.5
示例 2:
# R program to print rank of a vector values
# Creating a vector
x <- c(2, 5, 3, 6, -4, NA, 7, Inf, 5)
# Calling rank() function
rank(x)
# Calling with NA removed
rank(x, na.last = FALSE)
输出:
[1] 2.0 4.5 3.0 6.0 1.0 9.0 7.0 8.0 4.5
[1] 3.0 5.5 4.0 7.0 2.0 1.0 8.0 9.0 5.5