R语言 返回一个数字向量的第一个最小值的索引 – which.min()函数
R语言中的 which.min() 函数用于返回数字向量中第一个最小值的位置。
语法: which.min(x)
参数:
x: 数字向量
例1 :
# R program to find index of
# first minimum value
# Creating a vector
x <- c(2, 3, 4, 5, 1, 2, 3, 1, 2)
# Calling which.min() function
which.min(x)
# Printing minimum value
x[which.min(x)]
输出
[1] 5
[1] 1
例2 :
# R program to find index of
# first minimum value
# Calling pre-defined dataset
BODdemand
# Calling which.min() function
which.min(BODdemand)
# Printing minimum value
BODdemand[which.min(BODdemand)]
输出
[1] 8.3 10.3 19.0 16.0 15.6 19.8
[1] 1
[1] 8.3