Reputation: 33
I am very new at using R so my apologies in advance, if I ask something very obvious or if I use wrong terms. I hope you will still be able to help me. I have a list of values and I want to find not only the maximum (i.e. the highest value of the list), but also at which location/data point this maximum is.
E.g. this is the list called c_01:
[1,] 3
[2,] 5
[3,] 9
[4,] 4
[5,] 8
[6,] 7
[7,] 9
[8,] 7
[9,] 4
[10,] 3
I have tried with the code
max(c_01$acf)
But the output is always
[1] 9
I want to get an output which more or less tells me, that the maximum (9) can be found at [3,]. Is this possible?
Thank you in advance!
Upvotes: 3
Views: 200
Reputation: 96
How max() function can do for us: Returns the (regular or parallel) maxima and minima of the input values.
What you are trying to do is to locate the maximum value in your data frame. You should try which.max() function instead. That is:
which.max(c_01$acf)
The result then becomes
[1] 3
Caution: which.max() only determines the location, i.e., index of the (first) minimum or maximum of a numeric (or logical) vector. You can try the following syntax to locate all maximum values.
which(c_01$acf == max(c_01$acf))
The result will be
[1] 3 7
Upvotes: 1
Reputation: 13319
We could use:
paste0("Max is: ", do.call(max,c_01), " at index: ",which.max(c_01))
[1] "Max is: 9 at index: 3"
Or:
c(do.call(max,c_01),
paste("index: ",which(my_list==do.call(max,c_01),arr.ind = T)))
[1] "9" "index: 3" "index: 7"
Upvotes: 1