返回 R 编程中数值向量的第一个最大值的索引 – which.max()函数
R语言中的which.max()
函数用于返回数值向量中第一个最大值的位置。
Syntax: which.max(x)
Parameters:
x: Numeric Vector
示例 1:
# R program to find index of
# first maximum value
# Creating a vector
x <- c(2, 3, 4, 5, 1, 2, 3, 1, 2)
# Calling which.max() function
which.max(x)
# Printing maximum value
x[which.max(x)]
输出:
[1] 4
[1] 5
示例 2:
# R program to find index of
# first maximum value
# Calling pre-defined dataset
BOD$demand
# Calling which.max() function
which.max(BOD$demand)
# Printing maximum value
BOD$demand[which.max(BOD$demand)]
输出:
[1] 8.3 10.3 19.0 16.0 15.6 19.8
[1] 6
[1] 19.8