I have a list containing multiple members ( list1
). Each member in the list contains various numbers. I want to find the list member(s) that have at least 3 values greater than or equal to 5. How can I accomplish this?
aa <- c(1,2,3,4,5)
bb <- c(5,6,9,7,8)
cc <- c(3,8,5,1,6)
list1 <- list(aa,bb,cc)
In this case, I'd like to see bb
and cc
be returned. Thanks!
You can use Filter
to return a filtered object
Filter(function(x) sum(x>=5)>=3,list1)
Or if you want a "hadley" solution
devtools::install_github("hadley/purrr")
list1 %>% keep(function(x) sum(x>=5)>=3)
if you just want the index of the elements that satisfy your condition, use:
which(sapply(list1,function(x)sum(x>=5))>=3)
if you want the names of the list elements that match your condition, use:
names(list1)[sapply(list1,function(x)sum(x>=5))>=3]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.