Table of Contents
Removing columns with NA values in R can be achieved by using the “complete.cases()” function. This function allows you to identify and remove columns in a dataset that contain missing values. By specifying the dataset and using the “complete.cases()” function, R will automatically drop any columns with NA values, leaving you with a clean and complete dataset. This method is useful for data analysis and ensures that your results are accurate and reliable.
Remove Columns with NA Values in R
You can use one of the following two methods to remove columns from a data frame in R that contain NA values:
Method 1: Use Base R
df[ , colSums(is.na(df))==0]
Method 2: Use dplyr
library(dplyr) df %>% select_if(~ !any(is.na(.)))
Both methods produce the same result.
The following examples show how to use each method in practice with the following data frame:
#create data frame
df <- data.frame(team=c('A', 'B', 'C', 'D', 'E'),
points=c(99, NA, NA, 88, 95),
assists=c(33, 28, 31, 39, 34),
rebounds=c(30, 28, 24, 24, NA))
#view data frame
df
team points assists rebounds
1 A 99 33 30
2 B NA 28 28
3 C NA 31 24
4 D 88 39 24
5 E 95 34 NA
Example 1: Remove Columns with NA Values Using Base R
The following code shows how to remove columns with NA values using functions from base R:
#define new data frame new_df <- df[ , colSums(is.na(df))==0] #view new data frame new_df team assists 1 A 33 2 B 28 3 C 31 4 D 39 5 E 34
Notice that the two columns with NA values (points and rebounds) have both been removed from the data frame.
Example 2: Remove Columns with NA Values Using dplyr
The following code shows how to remove columns with NA values using functions from the package:
library(dplyr)
#define new data frame
new_df <- df %>% select_if(~ !any(is.na(.)))
#view new data frame
new_df
team assists
1 A 33
2 B 28
3 C 31
4 D 39
5 E 34
Once again, the two columns with NA values (points and rebounds) have both been removed from the data frame.
Cite this article
stats writer (2024). How can I remove columns with NA values in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-remove-columns-with-na-values-in-r/
stats writer. "How can I remove columns with NA values in R?." PSYCHOLOGICAL SCALES, 27 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-remove-columns-with-na-values-in-r/.
stats writer. "How can I remove columns with NA values in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-remove-columns-with-na-values-in-r/.
stats writer (2024) 'How can I remove columns with NA values in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-remove-columns-with-na-values-in-r/.
[1] stats writer, "How can I remove columns with NA values in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I remove columns with NA values in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
