英文:
How can I remove rows from a dataframe based on having NA in two columns?
问题
数据框叫做a。它有20列,共有4000行。
其中两列分别为ndc和ndtem。我需要删除那两列中有NA值的行,同时保留其他行。
我尝试了以下几种方法:
a <- a[complete.cases(a[, c("ndc", "ndtem")]), ]
a <- a[!(is.na(a$ndc) | is.na(a$ndtem)), ]
a <- a %>% filter(!is.na(ndc) & !is.na(ndtem))
请纠正我的代码或者提供替代方法。
英文:
The dataframe is called a. It has 4000 rows across 20 columns.
Two of the columns are called ndc and ndtem. I need to remove rows that have NA in those two columns and keep everything else
I tried these methods:
a<- a[complete.cases(a[, c("ndc", "ndtem")]), ]
a <- a[!(is.na(a$ndc) | is.na(a$ndtem)), ]
a <- a %>% filter(!is.na(ndc) & !is.na(ndtem))
Please correct my code or help me with an alternative.
答案1
得分: 0
你正在删除那些两列中的任一列具有 NA
值的行。
要删除那些两列都是 NA
的行,以下条件将起作用:
a[!(is.na(a$ndc) & is.na(a$ndtem)), ]
a[!is.na(a$ndc) | !is.na(a$ndtem), ]
使用 dplyr
:
library(dplyr)
a %>% filter(!(is.na(ndc) & is.na(ndtem)))
英文:
You are removing rows for which either of the two columns has an NA
.
To remove rows for which both columns are NA
the following conditions would work:
a[!(is.na(a$ndc) & is.na(a$ndtem)), ]
a[!is.na(a$ndc) | !is.na(a$ndtem), ]
With dplyr
:
library(dplyr)
a %>% filter(!(is.na(ndc) & is.na(ndtem)))
答案2
得分: 0
根据评论中的建议,我修复了我的NAs。我将它们分配为"NA"而不是只是NA。一旦我去掉引号,我使用了以下代码:
a <- a %>% filter(!is.na(ndc) & !is.na(ndtem))
这样就完美地运行了。
英文:
At the suggestion in the comments. I fixed my NAs. I had assigned them "NA" instead of just NA. Once I removed the quotation marks, I used
a <- a %>% filter(!is.na(ndc) & !is.na(ndtem))
And it worked perfectly.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论