Anastasia So
Anastasia So

Reputation: 17

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

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?

Upvotes: 0

Views: 4181

Answers (2)

Fursham Hamid
Fursham Hamid

Reputation: 21

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

Upvotes: 0

akrun
akrun

Reputation: 886938

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

Upvotes: 2

Related Questions