我有一个data.frames列表,如下所示:
$ 0
My_Patient_Name Control My_experiment
Patient1 <NA> <NA>
Patient2 <NA> <NA>
Patient3 <NA> YES
$ 1
My_Patient_Name Control My_experiment
Patient4 1,3,5 <NA>
Patient5 1,3,5 <NA>
Patient6 1,3,5 <NA>
我想检查列表的每个data.frame中是否在控件列中有NA。对于一个data.frame,它一次是:
condition = sapply(mydf[[1]][2],function(x) {!all(is.na(x))}
then
condition = sapply(mydf[[2]][2],function(x) {!all(is.na(x))}
and so on.
但是我想在列表的每个data.frame上运行它。
答案 0 :(得分:0)
使用lapply检查列表中每个元素的条件
mydf = list()
mydf[[1]] <- data.frame(My_Patient_Name=c("Patient1", "Patient2", "Patient3"), Control=c(NA, NA, NA), My_experiment=c(NA,NA,"YES"))
mydf[[2]] <- data.frame(My_Patient_Name=c("Patient3", "Patient4", "Patient5"), Control=c("1,3,5", "1,3,5", "1,3,5"), My_experiment=c(NA,NA,NA))
lapply(mydf, function(df) sapply(df[2],function(x) {any(is.na(x))}))
#[[1]]
#Control
# TRUE
#[[2]]
#Control
# FALSE