删除列R中包含NAs的行

时间:2022-12-17 20:10:21

I have a data.frame that contains many columns. I want to keep the rows that have no NAs in 4 of these columns. The complication arises from the fact that I have other rows that are allowed have NAs in them so I can't use complete.cases or is.na. What's the most efficient way to do this?

我有一个包含许多列的data.frame。我要保留这些列中没有NAs的行。复杂的是,我有其他行允许有NAs,所以我不能使用complete。用例或is.na。最有效的方法是什么?

1 个解决方案

#1


17  

You can still use complete.cases(). Just apply it to the desired columns (columns 1:4 in the example below) and then use the Boolean vector it returns to select valid rows from the entire data.frame.

仍然可以使用complete.cases()。只需将它应用到所需的列(下面示例中的第1 - 4列),然后使用它返回的布尔向量从整个data.frame中选择有效的行。

set.seed(4)
x <- as.data.frame(replicate(6, sample(c(1:10,NA))))
x[complete.cases(x[1:4]),]
#    V1 V2 V3 V4 V5 V6
# 1   7  4  6  8 10  5
# 2   1  2  5  5  1  2
# 5   6  8  4 10  6  6
# 6   2  6  9  3  4  4
# 7   4  3  3  1  2  1
# 9   8  5  2  7  7  3
# 10 10 10  1  2  5 NA

#1


17  

You can still use complete.cases(). Just apply it to the desired columns (columns 1:4 in the example below) and then use the Boolean vector it returns to select valid rows from the entire data.frame.

仍然可以使用complete.cases()。只需将它应用到所需的列(下面示例中的第1 - 4列),然后使用它返回的布尔向量从整个data.frame中选择有效的行。

set.seed(4)
x <- as.data.frame(replicate(6, sample(c(1:10,NA))))
x[complete.cases(x[1:4]),]
#    V1 V2 V3 V4 V5 V6
# 1   7  4  6  8 10  5
# 2   1  2  5  5  1  2
# 5   6  8  4 10  6  6
# 6   2  6  9  3  4  4
# 7   4  3  3  1  2  1
# 9   8  5  2  7  7  3
# 10 10 10  1  2  5 NA