不确定你是否可以这样做select_if
单独使用,但一种方法是将两个结合起来select
操作,然后绑定列。使用mtcars
作为样本数据。
library(dplyr)
bind_cols(mtcars %>% select_if(function(x) sum(x) > 1000),
mtcars %>% select(mpg, cyl))
# disp hp mpg cyl
#1 160.0 110 21.0 6
#2 160.0 110 21.0 6
#3 108.0 93 22.8 4
#4 258.0 110 21.4 6
#5 360.0 175 18.7 8
#6 225.0 105 18.1 6
#7 360.0 245 14.3 8
#8 146.7 62 24.4 4
#....
但是,如果列满足两个条件(在select_if
也select
)然后该列将被重复。
我们还可以使用基本 R 来提供相同的输出,但避免列被选择两次unique
.
sel_names <- c("mpg", "cyl")
mtcars[unique(c(sel_names, names(mtcars)[sapply(mtcars, sum) > 1000]))]
因此,对于您的情况,两个版本将是:
bind_cols(df_inh %>% select_if(function(x) sd(x) != 0),
df_inh %>% select(isolate, Phenotype))
and
sel_names <- c("isolate", "Phenotype")
df_inh[unique(c(sel_names, names(df_inh)[sapply(df_inh, sd) != 0]))]