简体   繁体   中英

Remove list from lists in list if length

I have this list:

a <- list(list(c("sam1", "control"), c("sam1", "latanoprost free acid", "GSM6683", "GSM6684"), c("sam1", "prostaglandin F2alpha", "GSM6687", "GSM6688")), list(c("sam2", "control"), c("sam2", "latanoprost free acid", "GSM6681", "GSM6682"), c("sam2", "prostaglandin F2alpha", "GSM6685", "GSM6686")))

I'd like to remove the elements (lists), which length are less than three (<3). I tried double lapply to get a[[i]][[j]] and <- NULL, but I got lists only with NULL. Like this:

b <- lapply(seq(length(a)),function(i){
  lapply(seq(length(a[[1]])),function(j){
    if(length(a[[i]][[j]]) < 3) {a[[i]][[j]] <- NULL}
  })
})

Thank you for any help...

How about this?

lapply(a, function(x) x[sapply(x, length) >= 3])

or

lapply(a, Filter, f = function(x) length(x) >= 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM