简体   繁体   English

na.omit删除R中的所有观测值

[英]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. 我正在尝试使用R中的na.omit()函数将我的所有教育变量的NA删除到我的数据中。但是,该函数删除了我在数据中的所有观察值,尽管教育变量只有两个NA 。 Below is the R output: 以下是R输出:

> 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? sum(is.na())函数仅计算两个NA,因此na.omit()应该只删除两行,对吗? 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)) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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