Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete columns that contain ONLY NAs?

Tags:

dataframe

r

na

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(...)  

specifying some additional arguments?

like image 735
Lorenzo Rigamonti Avatar asked Apr 12 '13 10:04

Lorenzo Rigamonti


People also ask

How do I remove columns containing Na in R?

To remove observations with missing values in at least one column, you can use the na. omit() function. The na. omit() function in the R language inspects all columns from a data frame and drops rows that have NA's in one or more columns.

How do I delete rows with all NAS?

To remove all rows having NA, we can use na. omit function. For Example, if we have a data frame called df that contains some NA values then we can remove all rows that contains at least one NA by using the command na. omit(df).

How do you remove columns from a data set?

The most easiest way to drop columns is by using subset() function. In the code below, we are telling R to drop variables x and z. The '-' sign indicates dropping variables. Make sure the variable names would NOT be specified in quotes when using subset() function.


1 Answers

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] 
like image 163
Ciarán Tobin Avatar answered Sep 19 '22 18:09

Ciarán Tobin