0
如何從列表中找到最低值?從列表中返回最低值[R]
我知道min()
作品與載體
v <- c(1,2,7,0,45,78,89,90,2,-1)
min(v)
> -1
,但它不與列表工作。
l <- list(1,2,7,0,45,78,89,90,2,-1)
min(l)
> Error in min(c) : invalid 'type' (list) of argument
如何從列表中找到最低值?從列表中返回最低值[R]
我知道min()
作品與載體
v <- c(1,2,7,0,45,78,89,90,2,-1)
min(v)
> -1
,但它不與列表工作。
l <- list(1,2,7,0,45,78,89,90,2,-1)
min(l)
> Error in min(c) : invalid 'type' (list) of argument
如何
min(unlist(list(1,2,7,0,45,78,89,90,2,-1)))
還有Reduce
這種情況:
它也將工作,如果列表中的元素並不都是長度爲1:
l2 <- list(a = c(1,-2), 2,7,0,45,78,89,90,2,-1)
Reduce(min, l)
# [1] -2
更好: sapply(l,min) – Chris
這對我不起作用;它只是再次吐出清單。 – bramtayl
你必須把矢量放在列表中作爲一個向量,而不是作爲一個多分量列表... l < - c(number,number,number)...然後sapply(l,min) – Chris