dplyr::当列名包含空格时,mutate_if失败
我有以下情况:我有一个tibble和一个字符向量,它包含了一些所说的tibbles列名。现在我使用dplyr::mutate_if将这些列设置为0,其名称包含在字符向量中。为此,我提供了一个逻辑向量作为谓词 到目前为止,这已经奏效:dplyr::当列名包含空格时,mutate_if失败,r,dplyr,R,Dplyr,我有以下情况:我有一个tibble和一个字符向量,它包含了一些所说的tibbles列名。现在我使用dplyr::mutate_if将这些列设置为0,其名称包含在字符向量中。为此,我提供了一个逻辑向量作为谓词 到目前为止,这已经奏效: library(tidyverse) test_OK <- tribble(~"Var1", ~"Var2", ~"Var3", 11, 12, 13, 21, 22, 23
library(tidyverse)
test_OK <-
tribble(~"Var1", ~"Var2", ~"Var3",
11, 12, 13,
21, 22, 23,
31, 32, 33)
vars_to_change_OK <- c("Var1", "Var3")
## Everything works out
test_OK %>% mutate_if(names(.) %in% vars_to_change_OK, funs(. * 0))
然而,我感兴趣的是为什么上面的内容在dplyr中不起作用
更新:我添加了sessionInfo,以防版本很重要:
R version 3.4.0 (2017-04-21)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
Matrix products: default
locale:
[1] LC_COLLATE=German_Germany.1252 LC_CTYPE=German_Germany.1252 LC_MONETARY=German_Germany.1252
[4] LC_NUMERIC=C LC_TIME=German_Germany.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] dplyr_0.5.0 purrr_0.2.2 readr_1.1.0 tidyr_0.6.1 tibble_1.3.0
[6] ggplot2_2.2.1 tidyverse_1.1.1 dygraphs_1.1.1.4 shiny_1.0.3 RevoUtilsMath_10.0.0
loaded via a namespace (and not attached):
[1] Rcpp_0.12.10 cellranger_1.1.0 compiler_3.4.0 plyr_1.8.4 forcats_0.2.0 tools_3.4.0 digest_0.6.12
[8] lubridate_1.6.0 jsonlite_1.4 nlme_3.1-131 gtable_0.2.0 lattice_0.20-35 psych_1.7.3.21 DBI_0.6-1
[15] parallel_3.4.0 haven_1.0.0 xml2_1.1.1 httr_1.2.1 stringr_1.2.0 hms_0.3 RevoUtils_10.0.4
[22] htmlwidgets_0.8 grid_3.4.0 R6_2.2.0 readxl_1.0.0 foreign_0.8-67 modelr_0.1.0 reshape2_1.4.2
[29] magrittr_1.5 scales_0.4.1 htmltools_0.3.6 rvest_0.3.2 assertthat_0.2.0 mnormt_1.5-5 colorspace_1.3-2
[36] mime_0.5 xtable_1.8-2 httpuv_1.3.3 stringi_1.1.5 lazyeval_0.2.0 munsell_0.4.3 broom_0.4.2
[43] zoo_1.8-0
我们可以使用mutate_at并将“vars_to_change_FAIL”放在vars中
谢谢你的解决方案!然而,我对我的代码失败的原因感兴趣/如果我不正确地使用mutate_if。@AEF您有哪个版本的dplyr?@AEF请将您的dplyr更新为0.7.0,它应该可以工作。我已将我的会话信息添加到问题中。这是dplyr 0.5.0。有什么特别的原因不使用mutate_吗?请注意,错误仍然存在remain@GGamba不,我当然可以换一种解决方案。然而,我不知道为什么它不起作用,甚至不知道它是否应该起作用。
###############################
## workaround in base R:
test_FAIL[, match(vars_to_change_FAIL, names(test_FAIL))] <- 0
test_FAIL
R version 3.4.0 (2017-04-21)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
Matrix products: default
locale:
[1] LC_COLLATE=German_Germany.1252 LC_CTYPE=German_Germany.1252 LC_MONETARY=German_Germany.1252
[4] LC_NUMERIC=C LC_TIME=German_Germany.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] dplyr_0.5.0 purrr_0.2.2 readr_1.1.0 tidyr_0.6.1 tibble_1.3.0
[6] ggplot2_2.2.1 tidyverse_1.1.1 dygraphs_1.1.1.4 shiny_1.0.3 RevoUtilsMath_10.0.0
loaded via a namespace (and not attached):
[1] Rcpp_0.12.10 cellranger_1.1.0 compiler_3.4.0 plyr_1.8.4 forcats_0.2.0 tools_3.4.0 digest_0.6.12
[8] lubridate_1.6.0 jsonlite_1.4 nlme_3.1-131 gtable_0.2.0 lattice_0.20-35 psych_1.7.3.21 DBI_0.6-1
[15] parallel_3.4.0 haven_1.0.0 xml2_1.1.1 httr_1.2.1 stringr_1.2.0 hms_0.3 RevoUtils_10.0.4
[22] htmlwidgets_0.8 grid_3.4.0 R6_2.2.0 readxl_1.0.0 foreign_0.8-67 modelr_0.1.0 reshape2_1.4.2
[29] magrittr_1.5 scales_0.4.1 htmltools_0.3.6 rvest_0.3.2 assertthat_0.2.0 mnormt_1.5-5 colorspace_1.3-2
[36] mime_0.5 xtable_1.8-2 httpuv_1.3.3 stringi_1.1.5 lazyeval_0.2.0 munsell_0.4.3 broom_0.4.2
[43] zoo_1.8-0
test_FAIL %>%
mutate_at(vars(vars_to_change_FAIL), funs(. * 0))
# A tibble: 3 x 3
# `Var 1` `Var 2` `Var 3`
# <dbl> <dbl> <dbl>
#1 0 12 0
#2 0 22 0
#3 0 32 0