你可以根据两列中是否有缺失值从数据框中删除行。

huangapple go评论62阅读模式
英文:

How can I remove rows from a dataframe based on having NA in two columns?

问题

数据框叫做a。它有20列,共有4000行。

其中两列分别为ndcndtem。我需要删除那两列中有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&lt;- a[complete.cases(a[, c(&quot;ndc&quot;, &quot;ndtem&quot;)]), ]

a &lt;- a[!(is.na(a$ndc) | is.na(a$ndtem)), ]

a &lt;- a %&gt;% filter(!is.na(ndc) &amp; !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) &amp; is.na(a$ndtem)), ]
a[!is.na(a$ndc) | !is.na(a$ndtem), ]

With dplyr:

library(dplyr)
a %&gt;% filter(!(is.na(ndc) &amp; 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.

huangapple
  • 本文由 发表于 2023年2月27日 06:42:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75575437.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定