Reputation: 5650
I have the following situation: I have a tibble and a character vector that conatins some of said tibbles column names. Now I use dplyr::mutate_if
to set those columns to 0, whose names are contained in the character vector. I do this by supplying a logical vector as predicate.
Up to now this has worked:
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))
But now I've come around a tibble with spaces and numbers in the column names and the procedure fails:
test_FAIL <-
tribble(~"Var 1", ~"Var 2", ~"Var 3",
11, 12, 13,
21, 22, 23,
31, 32, 33)
vars_to_change_FAIL <- c("Var 1", "Var 3")
## Error [...] unexpected numeric constant
test_FAIL %>% mutate_if(names(.) %in% vars_to_change_FAIL, funs(. * 0))
Note that the only difference are the column names which changed from Var[0-9]
to Var [0-9]
.
I'd like to know if this is a bug in dplyr
or if I have been using the mutate_if
function in an unintended way. Furthermore I'm interested in how and why the error occurs. Thanks in advance for any insights!
PS.: I am aware that there is an easy base R workaround like this:
###############################
## workaround in base R:
test_FAIL[, match(vars_to_change_FAIL, names(test_FAIL))] <- 0
test_FAIL
I am however interested in why the above doesn't work in dplyr
.
Update: I've added my sessionInfo()
in case the version is important:
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
Upvotes: 2
Views: 1628
Reputation: 887128
We can use mutate_at
and place the 'vars_to_change_FAIL' inside vars
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
Upvotes: 4