简体   繁体   English

从 R 中的数据集中删除 NA

[英]Removing NA’s from a dataset in R

I want to remove all of the NA's from the variables selected however when I used na.omited() for example:我想从选择的变量中删除所有 NA,但是当我使用na.omited() ,例如:

na.omit(df$livharm) 

it does not work and the NA's are still there.它不起作用,NA仍在那里。 I have also tried an alternative way for example:我也尝试了另一种方式,例如:

married[is.na(livharm1)] <-NA 

I have done this for each variable within the larger variable I am looking at using the code: Eg我已经使用代码为我正在查看的较大变量中的每个变量完成了此操作:例如

df <- within(df, { 
married <- as.numeric(livharm == 1) 
“
“
“ 

married[is.na(livharm1)] <- NA

})

however I'm not sure what I actually have to do.但是我不确定我实际上必须做什么。 Any help I would greatly appreciate!任何帮助我将不胜感激!

Using complete.cases gives:使用complete.cases给出:

dat <- data.frame( a=c(1,2,3,4,5),b=c(1,NA,3,4,5) )

dat
  a  b
1 1  1
2 2 NA
3 3  3
4 4  4
5 5  5

complete.cases(dat)
[1]  TRUE FALSE  TRUE  TRUE  TRUE

# is.na equivalent has to be used on a vector for the same result:
!is.na(dat$b)
[1]  TRUE FALSE  TRUE  TRUE  TRUE

dat[complete.cases(dat),]
  a b
1 1 1
3 3 3
4 4 4
5 5 5

Using na.omit is the same as complete.cases but instead of returning a boolean vector the object itself is returned.使用na.omitcomplete.cases相同,但不是返回布尔向量,而是返回对象本身。

na.omit(dat)
  a b
1 1 1
3 3 3
4 4 4
5 5 5

This function returns a different result when applied only to a vector, which is probably not handled correctly by ggplot2 .当仅应用于向量时,此函数返回不同的结果, ggplot2可能无法正确处理。 It can be "rescued" by putting it back in a data frame.可以通过将其放回数据框中来“拯救”它。 base plot works as intended though.基本plot虽然按预期工作。

na.omit(dat$b)
[1] 1 3 4 5
attr(,"na.action")
[1] 2
attr(,"class")
[1] "omit"

data.frame(b=na.omit(dat$b))
  b
1 1
2 3
3 4
4 5

Plotting with ggplot2使用ggplot2绘图

ggplot(dat[complete.cases(dat),]) + geom_point( aes(a,b) )
# <plot>

# See warning when using original data set with NAs
ggplot(dat) + geom_point( aes(a,b) )
Warning message:
Removed 1 rows containing missing values (geom_point).
# <same plot as above>

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

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