AwaitedOne
AwaitedOne

Reputation: 1012

Check if a column is na in a list

I am trying to check if any of the data frame in the list has a column with all entries NA.

i tried this lapply(df,function(x)all(is.na(df))) and works fine to check for all the columns. How can i check if in the below list, first column is NA.

I tried lapply(df[,1],function(x)all(is.na(df[,1]))) but its not a correct way to do

[[1]]
             ID Mas5.SignalIntensity DetectionCalls     P.value
1     1007_s_at           3242.90209              P 0.000218932
2       1053_at            377.81481              P 0.017000453
3        117_at            114.88743              A 0.066864977
4        121_at           8739.03257              P 0.000218932


[[2]]
             ID Mas5.SignalIntensity DetectionCalls     P.value
1            NA            134.40764              P 0.000561751
2            NA            453.34875              P 0.002227740
3            NA            706.34996              A 0.066864977
4            NA            102.51459              A 0.089405078

[[3]]
             ID Mas5.SignalIntensity DetectionCalls     P.value
1     1007_s_at          7015.297075              P 0.000218932
2       1053_at           677.459859              P 0.011447358
3        117_at           180.568654              A 0.267462560
4        121_at          1693.426847              P 0.006531992
5     1255_g_at           181.221325              A 0.339557900

Upvotes: 4

Views: 4253

Answers (1)

akrun
akrun

Reputation: 887991

Try

 sapply(lst, function(x) any(colSums(!is.na(x))==0))
 #[1]  TRUE FALSE  TRUE

Update

If you want to check for a particular column, for e.g. column 2

sapply(lst, function(x) all(is.na(x[,2])))
#[1] FALSE FALSE  TRUE

Or

sapply(lst, function(x) sum(!is.na(x[,2]))==0)
#[1] FALSE FALSE  TRUE

data

 df <- data.frame(col1= NA, col2=1:5, col3=c(1:3,NA, NA))
 df1 <- data.frame(col1=1:5, col2=6:10, col3=11:15)
 df2 <- data.frame(col1=c(NA,2), col2= NA, col3=c(2,4))
 lst <- list(df, df1, df2)

Upvotes: 4

Related Questions