【问题标题】:How do I find the values greater than or equal to a and then find the minimum of these greatest values in R?如何找到大于或等于 a 的值,然后在 R 中找到这些最大值中的最小值?
【发布时间】:2022-01-19 17:15:26
【问题描述】:
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

但它只给出 1.3772572 的值,而我需要 estFj 的所有值大于或等于 a,然后找到这些值中大于或等于 a 的最小值!

我该如何解决这个问题?

【问题讨论】:

    标签: r loops for-loop if-statement minimum


    【解决方案1】:

    我们可以使用向量化函数,找到大于aestFj 的值,然后找到它们的最小值。

    > 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
    

    【讨论】:

    • 由于编辑需要有一定的长度(我认为?),请在函数中添加“等于”。它应该是min(estFj[estFj&gt;=a]) 并且应该是“大于”而不是“小于”,符号与文本不匹配。
    猜你喜欢
    • 2016-09-30
    • 2018-06-19
    • 1970-01-01
    • 2012-12-28
    • 1970-01-01
    • 2012-12-02
    • 2015-09-24
    • 2012-06-21
    • 2016-03-16
    相关资源
    最近更新 更多