Find the minimum value with the longest distance between them

0

I have the following dataframe TrechoFim . Since TrechoFim$Dist is the distance from the X position, Y to the track axis. And each record (line) is separated every 1 meter.

            X        Y       Dist
2185 2009.418 509.0062 0.30954210
2186 2009.995 509.6386 0.25478011
2187 2010.770 510.4896 0.22718504
2188 2011.351 511.1273 0.16029461
2189 2012.131 511.9857 0.16602771
2190 2012.714 512.6290 0.06040384
2191 2013.300 513.2746 0.13259331
2192 2014.074 514.1303 0.04504868
2193 2014.663 514.7809 0.13168458
2194 2015.456 515.6550 0.16033795
2195 2016.050 516.3088 0.18749392
2196 2016.647 516.9638 0.27274667
2197 2017.451 517.8432 0.27937111
2198 2018.054 518.5007 0.32418454
2199 2018.854 519.3703 0.38011322
2200 2019.463 520.0303 0.40087549
2201 2020.075 520.6910 0.44604857
2202 2020.899 521.5777 0.49297011
...
2295 2086.820 587.0792 0.18834755
2296 2087.521 587.7147 0.13726732
2297 2088.222 588.3504 0.11221667
2298 2088.923 588.9865 0.12912719
2299 2089.862 589.8395 0.14652478
2300 2090.561 590.4766 0.10888046
2301 2091.261 591.1141 0.11009527
2302 2091.960 591.7519 0.14825829
2303 2092.659 592.3901 0.20350820
2304 2093.357 593.0288 0.26490837
2305 2094.293 593.8853 0.32066742

I made the following code (not elegant) to extract the values of TrechoFim$Dist close to zero. In my case, the first value that tends to zero

for (t in 2:length(TrechoFim$Dist)){
  if(TrechoFim$Dist[t]<0.12) {
    temp[t]<- ifelse((TrechoFim$Dist[t]< TrechoFim$Dist[t+1] & TrechoFim$Dist[t]< TrechoFim$Dist[t-1]), TrechoFim$Dist[t], NA)
  }
 temp<- data.frame(temp)
}

That resulted in:

 temp[2185:2305]

  [1]         NA         NA         NA         NA         NA 0.06040384         NA
  [8] 0.04504868         NA         NA         NA         NA         NA         NA
 [15]         NA         NA         NA         NA         NA         NA         NA
 [22]         NA         NA         NA         NA         NA         NA         NA
 [29]         NA         NA         NA         NA         NA         NA         NA
 [36]         NA         NA         NA         NA         NA         NA         NA
 [43]         NA         NA         NA         NA         NA         NA         NA
 [50]         NA         NA         NA         NA         NA         NA         NA
 [57]         NA         NA         NA         NA         NA         NA         NA
 [64]         NA         NA         NA         NA         NA         NA         NA
 [71]         NA         NA         NA         NA         NA         NA         NA
 [78]         NA         NA         NA         NA         NA         NA         NA
 [85]         NA         NA         NA         NA         NA         NA         NA
 [92]         NA         NA         NA         NA         NA         NA         NA
 [99]         NA         NA         NA         NA         NA         NA         NA
[106]         NA         NA         NA         NA         NA         NA         NA
[113] 0.11221667         NA         NA 0.10888046         NA         NA         NA
[120]         NA         NA

In the temp list I have the values that satisfy the if conditions, I need to extract the values 1 (0.06040384) and the value 3 (0.11221667) , since values 1 and 2 are close to each other and values 3 and 4 are also close together. However, around the value 1 there may be 3 or 4 values close and not only 1 as in the example. This situation is repeated throughout the DataFrame. If not clear, I can reformulate. I tried to simplify. Thank you!

    
asked by anonymous 27.02.2018 / 15:18

1 answer

0

I think you can make a subset sooner

newData <- subset(TrechoFim, TrechoFim$Dist < 0.12)

there you would have to loop to calculate the same distance.

    
27.02.2018 / 23:09