假设我们有一个具有六个观测值和四个变量的数据帧
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))
a | b | c | d||
---|---|---|---|---|
1 | NA | 1 | 6 | |
NA | NA | 2 | 7 | |
NA | NA | 3 | NA||
NA | ||||
NA | NA | NA | 4||
5 | 1 | 6 | 4 |
使用rowSums()
对每行中的NA进行计数。然后丢弃行中包含多个threshold*ncol(df)
NA的行。
threshold <- 0.5
df <- df[-which(rowSums(is.na(df)) > threshold*ncol(df)), ]