remove columns with NAs from all dataframes in list

前端 未结 2 1564
情书的邮戳
情书的邮戳 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)
    
    0 讨论(0)
  • 2021-01-12 18:37

    Try this

    lapply(WW1_Data, function(x) x[, !is.na(x)])
    $Alnön
      Site_Name     X1996     X2000      X2010
    1     Alnön 0.3076923 0.2608696 0.08333333
    
    $Ammarnäs
      Site_Name X1996 X2011
    2  Ammarnäs  0.75   0.8
    ...
    
    0 讨论(0)
提交回复
热议问题