How to delete columns that contain ONLY NAs?

后端 未结 7 1132
走了就别回头了
走了就别回头了 2020-11-28 04:36

I have a data.frame containing some columns with all NA values, how can I delete them from the data.frame.

Can I use the function

na.omit(...) 
         


        
7条回答
  •  时光说笑
    2020-11-28 04:54

    One way of doing it:

    df[, colSums(is.na(df)) != nrow(df)]
    

    If the count of NAs in a column is equal to the number of rows, it must be entirely NA.

    Or similarly

    df[colSums(!is.na(df)) > 0]
    

提交回复
热议问题