R Beginner here.
I have a list of class references. Each class has a field, “x”. I want to find the class reference which has the lowest “x” in the list.
In python I would do this:
return min(item.x for item in myList)
I’m not sure if there is specific terminology for the type of statement above (if someone knows I would like to know), but is there are way of doing a similar type of thing in R ?
If not, what would be the best way to achieve this in R ?
** Edit re. Justins comment
Here is how the list is created ..
mylist <- list()
for (i in 1:10){
mylist <- c(mylist, MyClass$new())
}
where:
MyClass <- setRefClass("MyClass",
fields = list(x = "numeric"),
methods = list(
initialize = function(){
x <<- sample(0:100, 1)
}
)
)
Many thanks
If you want to get to the entire instance of MyClass with the lowest value of x, as opposed to just the minimum value of x (you seem to be looking for the former), consider using
which.minin something like the following: