【发布时间】:2018-12-18 16:42:04
【问题描述】:
有很多关于使用 dplyr 的select_if 处理多种情况的帖子。但是,无论以何种方式,选择 is.factor 和变量名称到目前为止都对我不起作用。
最终,我想选择 df/tibble 中的所有因素并按名称排除某些变量。
例子:
df <- tibble(A = factor(c(0,1,0,1)),
B = factor(c("Yes","No","Yes","No")),
C = c(1,2,3,4))
各种尝试:
尝试 1
df %>%
select_if(function(col) is.factor(col) & !str_detect(names(col), "A"))
Error in selected[[i]] <- .p(.tbl[[tibble_vars[[i]]]], ...) : replacement has length zero
尝试 2
df %>%
select_if(function(col) is.factor(col) & negate(str_detect(names(col)), "A"))
Error: Can't convert a logical vector to function Call `rlang::last_error()` to see a backtrace
尝试 3
df %>%
select_if(function(col) is.factor(col) && !str_detect(names(col), "A"))
Error: Only strings can be converted to symbols Call `rlang::last_error()` to see a backtrace
尝试 4
df %>%
select_if(is.factor(.) && !str_detect(names(.), "A"))
Error in tbl_if_vars(.tbl, .predicate, caller_env(), .include_group_vars = TRUE) : length(.p) == length(tibble_vars) is not TRUE
同时,个别条件工作没有问题:
> df %>%
+ select_if(is.factor)
# A tibble: 4 x 2
A B
<fct> <fct>
1 0 Yes
2 1 No
3 0 Yes
4 1 No
> df %>%
+ select_if(!str_detect(names(.), "A"))
# A tibble: 4 x 2
B c
<fct> <dbl>
1 Yes 1
2 No 2
3 Yes 3
4 No 4
问题可能出在这里:
df %>%
select_if(function(col) !str_detect(names(col), "A"))
Error in selected[[i]] <- .p(.tbl[[tibble_vars[[i]]]], ...) : replacement has length zero
但是,我不知道如何解决这个问题。
【问题讨论】:
-
使用 %>% 运算符:df %>% dplyr::select_if(is.factor) %>% dplyr::select(-one_of('A'))
-
我认为自 this question/answer 以来情况没有改变。另见this。
-
我尝试了您提到的帖子中的解决方案,但它们似乎不适用于这个特定问题。我不知道为什么。