简体   繁体   中英

na.omit dropping all observations in R

I am trying to drop all of my NAs for an education variable in my data using the na.omit() function in R. However, the function drops all of my observations in the data, although there are only two NAs for the education variable. Below is the R output:

> dim(data)
[1] 146688    167
> sum(is.na(data$educ))
[1] 2
> data2 = na.omit(data$educ)
> dim(data2)
NULL

The sum(is.na()) function counts only two NAs, so na.omit() should only drop two rows, correct? Why is the function dropping all of my observations?

One easy way to do this is to subset your data. Also, you may want to try use the table function to see if the variable is missing.

table(is.na(data$educ))
test <- subset(data, is.na(educ)) # So you can look at the 2 observations missing this variable
data2 <- subset(data, !is.na(educ)) 

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