remove columns with NAs from all dataframes in list

前端 未结 2 1566
情书的邮戳
情书的邮戳 2021-01-12 18:20

I have a list made up of several data frames. I would like to remove all of the columns with NAs in each data frame. Note the columns to be removed are not the same in each

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-12 18:34

    Julius' answer is great, but since I already started writing...

    Your question is a little ambiguous, but if you want to remove any row with an NA from each data.frame in your list:

    lapply(WW1_Data, na.omit)
    

    Or you can use your own function, assuming each data.frame in your list only has one row like these do:

    myfun <- function(x) {
      x[, !is.na(x)]
    }
    
    lapply(WW1_Data, myfun)
    

    Or switch to a single data.frame, melt it and remove rows:

    out <- do.call(rbind, WW1_Data)
    out.m <- melt(out, id.vars='Site_Name')
    na.omit(out.m)
    

提交回复
热议问题