r语言 - 保留 NA 为 <= 总变量 20% 的观测值



假设我们有一个具有六个观测值和四个变量的数据帧

df <- data.frame(a = c(1, NA, NA, 4, NA, 5),
b = c(NA, NA, NA, NA, NA, 1),
c = c(1, 2, 3, 4, NA, 6),
d = c(6, 7, NA, NA, 4, 4))
dNA4
abc
1NA16
NANA27
NANA3
NA
NANANA
5164

使用rowSums()对每行中的NA进行计数。然后丢弃行中包含多个threshold*ncol(df)NA的行。

threshold <- 0.5
df <- df[-which(rowSums(is.na(df)) > threshold*ncol(df)), ]

最新更新