Finding the Location of Maximum and Minimum Values in R
Code
set.seed(150421)
x<-sample(100,10); x
which.max(x)
which.min(x)
When working with data for statistical purposes, it’s not only important to know what the maximum and minimum values are, but also necessary to identify their positions. This is particularly true for time series data.
Of course, R doesn’t need such functions for manipulation to be easy, but it’s generally better to avoid complex code if possible. It’s fine to use which(max(x)==x)
to find where the maximum value of data x
is. However, as code lengthens, variable names tend to get longer too, and it becomes problematic if every minor modification requires double the effort.