How do I find the values greater than or equal to a and then find the minimum of these greatest values in R?

Viewed 282
a <- 0.6875
estFj <- c(-0.8231686, 0.1237904, 1.1967068, 1.3772572)

#What I tried so far is: 

for(i in estFj) {
  if (i >= a) { 
    t = c(i)
  }
}

t

But it only gives the value of 1.3772572 whereas I need all the values of estFj greater than or equal to a and then find the minimum of these values that are greater than or equal to a!

How do I fix this?

1 Answers

We can use vectorised functions, find the values of estFj greater than a and then the minimum value of those.

> estFj => a # returns a logical vector
[1] FALSE FALSE  TRUE  TRUE
estFj[estFj => a]
[1] 1.196707 1.377257 # uses the logical vector to subset the original vector
> min(estFj[estFj >= a]) # take the minimum of this
[1] 1.196707
Related