在类列表中查找具有最小属性“X”的列表元素

时间:2022-09-08 22:49:58

How can I possibly identify which is the element in a list of classes, that has the min attribute X, among the others attributes X in the list?

如何识别列表中具有min属性X的类中的哪个元素,以及列表中的其他属性X?

Let me show you what I mean, in a more concrete way.

让我以更具体的方式向您展示我的意思。

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

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

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

The question mark take place the code that I wish to know how to implement. I have tried, with no good results what so ever, this way: min(l[]@x)

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

This would make me know which is the minimum value, among them all, and then search for the index that has it.

这将使我知道哪个是最小值,其中包括所有,然后搜索具有它的索引。

Because I could not even find the minimum value, I'm completely lost. Could you please help me find a solution?

因为我甚至找不到最小值,所以我完全迷失了。你能帮我找一个解决方案吗?

1 个解决方案

#1


0  

It seems that a way of solving it, with not that much computational cost is by executing for

似乎解决它的方法,没有那么多的计算成本是通过执行

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

pos = which(c == min(c))

#1


0  

It seems that a way of solving it, with not that much computational cost is by executing for

似乎解决它的方法,没有那么多的计算成本是通过执行

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

pos = which(c == min(c))