Home > Enterprise >  How to remove NA values in a specific column of a dataframe in R?
How to remove NA values in a specific column of a dataframe in R?

Time:07-04

I have a data frame with a large number of observations and I want to remove NA values in 1 specific column while keeping the rest of the data frame the same. I want to do this without using na.omit(). How do I do this?

CodePudding user response:

We can use is.na or complete.cases to return a logical vector for subsetting

subset(df1, complete.cases(colnm))

where colnm is the actual column name

CodePudding user response:

This is how I would do it using dplyr:

library(dplyr) 

df <- data.frame(a = c(1,2,NA), 
                 b = c(5,NA,8))
filter(df, !is.na(a))

# output 
a  b
1 1  5
2 2 NA
  • Related