我有这个清单:
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}
})
})
感谢您的帮助......
答案 0 :(得分:7)
这个怎么样?
lapply(a, function(x) x[sapply(x, length) >= 3])
或
lapply(a, Filter, f = function(x) length(x) >= 3)