Lorenzo Rigamonti Lorenzo Rigamonti - 1 year ago 130
R Question

How to delete columns that contain ONLY NAs?

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


specifying some additional arguments?

Answer Source

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,]#just a minor typo
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download