Table of Contents
The process of removing rows with NA (not available) values in one specific column in R involves identifying the column with NA values and using the “complete.cases” function to filter out those rows. This function creates a logical vector that identifies which rows are complete (without any NA values) and those that are not. By subsetting the data frame with this logical vector, the rows with NA values in the specified column can be removed. This ensures that the data remains accurate and complete for further analysis.
Remove Rows with NA in One Specific Column in R
You can use one of the following three methods to remove rows with NA in one specific column of a data frame in R:
#use is.na() method df[!is.na(df$col_name),] #use subset() method subset(df, !is.na(col_name)) #use tidyr method library(tidyr) df %>% drop_na(col_name)
Note that each of these methods will produce the same results.
The following examples show how to use each of these methods in practice with the following data frame:
#create data frame df <- data.frame(a = c(NA, 14, 19, 22, 26), b = c(14, NA, 9, NA, 5), c = c(45, 56, 54, 57, 59)) #view data framedf a b c 1 NA 14 45 2 14 NA 56 3 19 9 54 4 22 NA 57 5 26 5 59
Method 1: Remove Rows with NA Using is.na()
The following code shows how to remove rows from the data frame with NA values in a certain column using the is.na() method:
#remove rows from data frame with NA values in column 'b' df[!is.na(df$b),] a b c 1 NA 14 45 3 19 9 54 5 26 5 59
Method 2: Remove Rows with NA Using subset()
The following code shows how to remove rows from the data frame with NA values in a certain column using the subset() method:
#remove rows from data frame with NA values in column 'b' subset(df, !is.na(b)) a b c 1 NA 14 45 3 19 9 54 5 26 5 59
Method 3: Remove Rows with NA Using drop_na()
The following code shows how to remove rows from the data frame with NA values in a certain column using the drop_na() method:
library(tidyr)
#remove rows from data frame with NA values in column 'b'
df %>% drop_na(b)
a b c
1 NA 14 45
3 19 9 54
5 26 5 59Notice that each of the three methods produced the same result.
Note: You can find the complete online documentation for the drop_na() method .
Cite this article
stats writer (2024). How can I remove rows with NA in one specific column in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-remove-rows-with-na-in-one-specific-column-in-r/
stats writer. "How can I remove rows with NA in one specific column in R?." PSYCHOLOGICAL SCALES, 3 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-remove-rows-with-na-in-one-specific-column-in-r/.
stats writer. "How can I remove rows with NA in one specific column in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-remove-rows-with-na-in-one-specific-column-in-r/.
stats writer (2024) 'How can I remove rows with NA in one specific column in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-remove-rows-with-na-in-one-specific-column-in-r/.
[1] stats writer, "How can I remove rows with NA in one specific column in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.
stats writer. How can I remove rows with NA in one specific column in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
