【问题标题】:Find the list element, in a list of classes, that has the minimum attribute `X`在类列表中找到具有最小属性“X”的列表元素
【发布时间】:2018-04-21 12:21:59
【问题描述】:

我如何才能确定类列表中的哪个元素具有最小属性 X,以及列表中的其他属性 X?

让我以更具体的方式告诉你我的意思。

# class setup
setClass(
   "Stackoverflow",
   slots = list(
     x = "numeric",
     y = "numeric"
  )
)

#generate the list of classes
l = list()
for (i in 1:10)
{
  l[[i]] <- myLove4U <- new("Stackoverflow", 
                             x = runif(1, min=0, max=100), 
                             t = runif(1, min=0, max=100)  
                            )  
}

#find which is the element that has the smallest x
# ??

问号是我想知道如何实现的代码。 我试过了,没有什么好的结果,这样:min(l[]@x)

这会让我知道其中的最小值,然后搜索具有它的索引。

因为我什至找不到最小值,所以我完全迷失了。你能帮我找到解决办法吗?

【问题讨论】:

    标签: r list class min


    【解决方案1】:

    似乎解决它的一种方法是通过执行 for

    c = c()
    for (i in 1:length(l))
    {
        c[i] = l[[i]]@x
    }
    
    pos = which(c == min(c))
    

    【讨论】:

      猜你喜欢
      • 2023-03-30
      • 1970-01-01
      • 2010-11-30
      • 2017-07-20
      • 1970-01-01
      • 1970-01-01
      • 2020-04-04
      • 2020-06-11
      • 2020-02-10
      相关资源
      最近更新 更多