问题描述:

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)) 
相关阅读:
Top