我会用dplyr
here.
如果你想使用select()
有了逻辑条件,您可能正在寻找where()
选择助手dplyr
。
它可以这样使用:select(where(condition))
我使用 80% 阈值,因为 90% 将保留所有列,因此也不会说明解决方案
library(dplyr)
df %>% select(where(~mean(is.na(.))<0.8))
也可以使用 base R 和 colMeans 来完成:
df[, c(TRUE, colMeans(is.na(df[-1]))<0.8)]
或者用咕噜声:
library(purrr)
df %>% keep(~mean(is.na(.))<0.8)
Output:
gene cell1 cell3
1 a 0.4 NA
2 b NA 0.1
3 c 0.4 0.5
4 d NA 0.5
5 e 0.5 0.6
6 f 0.6 NA
Data
df<-data.frame(gene=letters[1:6],
cell1=c(0.4, NA, 0.4, NA, 0.5, 0.6),
cell2=c(0.1, rep(NA, 5)),
cell3=c(NA, 0.1, 0.5, 0.5, 0.6, NA))