简体   繁体   中英

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?

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

subset(df1, complete.cases(colnm))

where colnm is the actual column name

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM