Home > Software design >  Compare values in vector and if next value is greater or equal to any of previous values then make i
Compare values in vector and if next value is greater or equal to any of previous values then make i

Time:06-21

In the vector below, I want to compare values in the vector and if the next value is greater or equal to all the previous values in vec, then I want to make it NA

vec = c(14.2, 3.7, 2.875, 2.175, 1.575, 1.1, 0.7, 0.475, 0.3, 0.65, 0.125, 0.925, 0.025, 0.025, 0.015, 0.020)

Following is the expected output

vec = c(14.2, 3.7, 2.875, 2.175, 1.575, 1.1, 0.7, 0.475, 0.3, NA, 0.125, NA, 0.025, NA, 0.015, NA)

Following is the performance of the solutions below -

microbenchmark::microbenchmark(
TarJae = vec[c(which(vec >= shift(vec, n = 1L, type = "lag")))] <- NA,
onyambu = replace(vec, vec > cummin(vec) | duplicated(vec), NA),
times = 1000)

    expr     min       lq      mean   median       uq      max neval cld
  TarJae  41.846  44.1550  49.79315  46.2705  47.7505 2742.726  1000  b 
 onyambu  18.066  19.4435  21.21144  20.3280  21.3395   69.071  1000 a  

CodePudding user response:

As noted by @onyambu this solution will not work in case of **10, 12, 11**

library(dplyr) vec[c(which(vec >= lag(vec)))] <- NA

Update to the updated question: basically it is the same answer:

library(dplyr)

vec = c(14.2, 3.7, 2.875, 2.175, 1.575, 1.1, 0.7, 0.475, 0.3, 0.65, 0.125, 0.925, 0.025, 0.025, 0.015, 0.020)

vec[c(which(vec >= lag(vec)))] <- NA
vec

[1] 14.200  3.700  2.875  2.175  1.575  1.100  0.700  0.475  0.300     NA  0.125     NA  0.025     NA  0.015     NA

We could use lag then compare both vectors , get the index with which and assign NA to this indices:

library(dplyr)

vec[c(which(vec > lag(vec)))] <- NA

[1] 14.200  3.700  2.875  2.175  1.575  1.100  0.700  0.475  0.300     NA  0.125     NA

CodePudding user response:

One way to do this is:

replace(vec, vec > cummin(vec) | duplicated(vec), NA)
[1] 14.200  3.700  2.875  2.175  1.575  1.100  0.700  0.475  0.300     NA  0.125     NA  0.025  NA

or even:

`is.na<-`(vec, vec > cummin(vec)|duplicated(vec))
[1] 14.200  3.700  2.875  2.175  1.575  1.100  0.700  0.475  0.300     NA  0.125     NA  0.025  NA

CodePudding user response:

Try this

ifelse(c(vec[2]-vec[1] , diff(vec)) < 0 , vec , NA)
  •  Tags:  
  • r
  • Related