问题描述
我想知道如何在数据框中省略NA
值,但仅在我感兴趣的某些列中.
I want to know how to omit NA
values in a data frame, but only in some columns I am interested in.
例如,
DF <- data.frame(x = c(1, 2, 3), y = c(0, 10, NA), z=c(NA, 33, 22))
但是我只想省略y
为NA
的数据,因此结果应该是
but I only want to omit the data where y
is NA
, therefore the result should be
x y z
1 1 0 NA
2 2 10 33
na.omit
似乎删除所有包含任何NA
的行.
na.omit
seems delete all rows contain any NA
.
有人可以帮我解决这个简单的问题吗?
Can somebody help me out of this simple question?
但是如果现在我将问题更改为:
But if now I change the question like:
DF <- data.frame(x = c(1, 2, 3,NA), y = c(1,0, 10, NA), z=c(43,NA, 33, NA))
如果我只想省略x=na
或z=na
,我可以在哪里将|
放在函数中?
If I want to omit only x=na
or z=na
, where can I put the |
in function?
推荐答案
您可以使用complete.cases
函数并将其放入函数中:
You could use the complete.cases
function and put it into a function thusly:
DF <- data.frame(x = c(1, 2, 3), y = c(0, 10, NA), z=c(NA, 33, 22))
completeFun <- function(data, desiredCols) {
completeVec <- complete.cases(data[, desiredCols])
return(data[completeVec, ])
}
completeFun(DF, "y")
# x y z
# 1 1 0 NA
# 2 2 10 33
completeFun(DF, c("y", "z"))
# x y z
# 2 2 10 33
仅返回没有NA
s
Only return rows with no NA
s
如果要消除所有列中至少包含一个NA
的所有行,只需直接使用complete.cases
函数:
If you want to eliminate all rows with at least one NA
in any column, just use the complete.cases
function straight up:
DF[complete.cases(DF), ]
# x y z
# 2 2 10 33
或者如果completeFun
已经根深蒂固在您的工作流程中;)
Or if completeFun
is already ingrained in your workflow ;)
completeFun(DF, names(DF))
这篇关于省略包含特定NA列的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!