如果长度,从列表中的列表中删除列表

时间:2013-07-23 21:51:02

标签: r list if-statement remove-if

我有这个清单:

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")))

我想删除长度小于3(<3)的元素(列表)。我尝试了双重lapply来获得[[i]] [[j]]和&lt; - NULL,但我只获得了NULL列表。 像这样:

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

感谢您的帮助......

1 个答案:

答案 0 :(得分:7)

这个怎么样?

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

lapply(a, Filter, f = function(x) length(x) >= 3)