I would like to know if there is an R way (one liner) to extract the coordinates of all subsets of a vector that are above a given threshold. Suppose I have the following data:
v = c(3.48, 2.59, 1.73, 0.91, 0.13, -0.63, -1.34, -2.03, -2.67, -3.28, -3.04, -2.15, -1.20, -0.19, 0.84, 1.86, 2.84, 3.77, 4.60, 5.31, 4.16, 2.87, 1.89, 0.51, 0.23, 0.78, 1.34, 2.63, 1.72, 0.62, 0.98, 1.45)
and let's say I have threshold = 0.7
. The desired output would be:
left right
1 4
15 23
26 29
31 32
I can in principle write a while
loop or some sort, subsetting v
and juggling with left
and right
coordinates of these regions, something like:
left = which(subset >= threshold)[1] + right
right = which(subset[left:length(subset)] < threshold)[1] - 1 # -1 to get the last element above the threshold
subset = v[(right + 1):length(v)]
(not tested), but I am sure there is an R way that i can't seem to remember.
I had a look here but it's not really what I am after. Any help is appreciated.