Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Minimum and maximum sequential values of a vector

I need to find the minimum and the maximum values of a vector, but the minimum one needs to be positioned inside before the maximum one. So, given the vector:

v <- c (8,2,1,3,5,7,4)

the minimum value would be '1' (the 3rd element) and the maximum '7' (the sixth element). The maximum value would not be indeed '8' (the 1th element) because it occurs before the minimum '1' (the 3rd element).

I looked at the functions 'min' and 'max' (and their faster versions pmin and pmax), but apparently they are only able to find the highest minimum and maximum values, with no restrictions in place. The same for the range function.

Is there anyone that could help?

like image 313
Arturo Avatar asked Jan 25 '23 07:01

Arturo


1 Answers

You can do:

range(v[which.min(v):length(v)])

[1] 1 7
like image 200
tmfmnk Avatar answered Jan 30 '23 05:01

tmfmnk