简体   繁体   English

从R中的数据框中删除行时NA值出现问题

[英]Issue with NA values when removing rows from data frame in R

This is my data frame: 这是我的数据框:

ID <- c('TZ1','TZ2','TZ3','TZ4')
hr <- c(56,32,38,NA)
cr <- c(1,4,5,2)
data <- data.frame(ID,hr,cr)

   ID hr cr
1 TZ1 56  1
2 TZ2 32  4
3 TZ3 38  5
4 TZ4 NA  2

I want to remove the rows where data$hr = 56. This is what I want the end product to be: 我想删除data $ hr = 56的行。这就是我想要的最终产品是:

   ID hr cr
2 TZ2 32  4
3 TZ3 38  5
4 TZ4 NA  2

This is what I thought would work: 我认为这是可行的:

data = data[data$hr !=56,]

However the resulting data frame looks like this: 但是,结果数据帧如下所示:

     ID hr cr
2   TZ2 32  4
3   TZ3 38  5
NA <NA> NA NA

How can I mofify my code to encorporate the NA value so this doesn't happen? 如何修改代码以包含NA值,这样就不会发生? Thank you for your help, I can't figure it out. 谢谢您的帮助,我无法解决。

EDIT: I also want to keep the NA value in the data frame. 编辑:我也想将NA值保留在数据框中。

The issue is that when we do the == or != , if there are NA values, it will remain as such and create an NA row for that corresponding NA value. 问题是,当我们执行==!= ,如果存在NA值,它将保持不变并为该对应的NA值创建NA行。 So one way to make the logical index with only TRUE/FALSE values will be to use is.na also in the comparison. 因此,使逻辑索引仅包含TRUE / FALSE值的一种方法是在比较中也使用is.na

 data[!(data$hr==56 & !is.na(data$hr)),]
 #   ID hr cr
 #2 TZ2 32  4
 #3 TZ3 38  5
 #4 TZ4 NA  2

We could also apply the reverse logic 我们也可以应用反向逻辑

subset(data, hr!=56|is.na(hr))
#   ID hr cr
#2 TZ2 32  4
#3 TZ3 38  5
#4 TZ4 NA  2

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

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