R3.6中的mutate_at()问题
我一直在使用R3.6中的mutate_at()问题,r,dplyr,R,Dplyr,我一直在使用dplyr::mutate_at(),没有任何问题。但是在更新到R3.6之后,它会给出一条错误消息。比如说, library(dplyr) salary <- structure( list(S = c(13876, 11608, 18701, 11283, 11767, 20872), X = c(1, 1, 1, 1, 1, 2), E = c(1, 3, 3, 2, 3, 2), M = c(1, 0, 1, 0, 0, 1)), row.names
dplyr::mutate_at()
,没有任何问题。但是在更新到R3.6之后,它会给出一条错误消息。比如说,
library(dplyr)
salary <-
structure(
list(S = c(13876, 11608, 18701, 11283, 11767, 20872), X = c(1, 1, 1, 1, 1, 2), E = c(1, 3, 3, 2, 3, 2), M = c(1, 0, 1, 0, 0, 1)),
row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame")
)
salary
#> # A tibble: 6 x 4
#> S X E M
#> <dbl> <dbl> <dbl> <dbl>
#> 1 13876 1 1 1
#> 2 11608 1 3 0
#> 3 18701 1 3 1
#> 4 11283 1 2 0
#> 5 11767 1 3 0
#> 6 20872 2 2 1
我可以在3.5.3
版本中获得此输出
sessionInfo()
#> R version 3.5.3 (2019-03-11)
#> Platform: x86_64-apple-darwin15.6.0 (64-bit)
#> Running under: macOS Mojave 10.14.5
#>
#> Matrix products: default
#> BLAS: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRblas.0.dylib
#> LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib
#>
#> locale:
#> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
#>
#> attached base packages:
#> [1] stats graphics grDevices utils datasets methods base
#>
#> other attached packages:
#> [1] dplyr_0.8.0.1
#>
#> loaded via a namespace (and not attached):
#> [1] Rcpp_1.0.1 fansi_0.4.0 utf8_1.1.4 crayon_1.3.4
#> [5] digest_0.6.18 assertthat_0.2.1 R6_2.4.0 magrittr_1.5
#> [9] evaluate_0.13 pillar_1.3.1 cli_1.1.0 rlang_0.3.3
#> [13] stringi_1.4.3 rmarkdown_1.12 tools_3.5.3 stringr_1.4.0
#> [17] glue_1.3.1 purrr_0.3.2 xfun_0.5 yaml_2.2.0
#> [21] compiler_3.5.3 pkgconfig_2.0.2 htmltools_0.3.6 tidyselect_0.2.5
#> [25] knitr_1.22 tibble_2.1.1
salary %>%
mutate_at(.vars = vars("E", "M"), .funs = list(factor))
#> # A tibble: 6 x 4
#> S X E M
#> <dbl> <dbl> <fct> <fct>
#> 1 13876 1 1 1
#> 2 11608 1 3 0
#> 3 18701 1 3 1
#> 4 11283 1 2 0
#> 5 11767 1 3 0
#> 6 20872 2 2 1
salary %>%
mutate_at(.vars = vars("E", "M"), .funs = list(~factor(.)))
#> # A tibble: 6 x 4
#> S X E M
#> <dbl> <dbl> <fct> <fct>
#> 1 13876 1 1 1
#> 2 11608 1 3 0
#> 3 18701 1 3 1
#> 4 11283 1 2 0
#> 5 11767 1 3 0
#> 6 20872 2 2 1
3.6
但是,当我使用r3.6
版本在另一个笔记本上运行完全相同的代码时
sessionInfo()
#> R version 3.6.0 (2019-04-26)
#> Platform: x86_64-apple-darwin15.6.0 (64-bit)
#> Running under: macOS Mojave 10.14.5
#>
#> Matrix products: default
#> BLAS: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRblas.0.dylib
#> LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib
#>
#> locale:
#> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
#>
#> attached base packages:
#> [1] stats graphics grDevices utils datasets methods base
#>
#> other attached packages:
#> [1] dplyr_0.8.1
#>
#> loaded via a namespace (and not attached):
#> [1] Rcpp_1.0.1 knitr_1.23 magrittr_1.5 tidyselect_0.2.5
#> [5] R6_2.4.0 rlang_0.3.4 fansi_0.4.0 stringr_1.4.0
#> [9] tools_3.6.0 xfun_0.7 utf8_1.1.4 cli_1.1.0
#> [13] htmltools_0.3.6 yaml_2.2.0 assertthat_0.2.1 digest_0.6.18
#> [17] tibble_2.1.1 crayon_1.3.4 purrr_0.3.2 vctrs_0.1.0
#> [21] zeallot_0.1.0 glue_1.3.1 evaluate_0.13 rmarkdown_1.12
#> [25] stringi_1.4.3 compiler_3.6.0 pillar_1.4.0 backports_1.1.4
#> [29] pkgconfig_2.0.2
我收到以下错误消息
salary %>%
mutate_at(.vars = vars("E", "M"), .funs = list(~factor))
#> Error: Column `E` is of unsupported type function
此错误发生在我几天前升级
R
后。这是否真的是由于升级R
或者还有其他原因吗?试试
salary%>%mutate\u at(.vars=vars(“E”,“M”),factor)
。使用list()
包装器(可能)是不必要的(或正确的)。我自己写答案,因为我知道了一些事情
我不确定错误为什么会发生在这里而不是那里,但我开始了解为什么在仔细阅读帮助文件后,list()
wrapper无法工作
我不应该把语法写成list(~function)
。在?mutate_all
的多重转换部分中,它说
# If you want to apply multiple transformations, pass a list of
# functions. When there are multiple functions, they create new
# variables instead of modifying the variables in place:
iris %>% mutate_if(is.numeric, list(scale2, log))
# The list can contain purrr-style formulas:
iris %>% mutate_if(is.numeric, list(~scale2(.), ~log(.)))
换句话说,list(factor)
和list(~factor(.))
可能适合使用list
包装器。这在最新的3.6.0
版本中有效
sessionInfo()
#> R version 3.5.3 (2019-03-11)
#> Platform: x86_64-apple-darwin15.6.0 (64-bit)
#> Running under: macOS Mojave 10.14.5
#>
#> Matrix products: default
#> BLAS: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRblas.0.dylib
#> LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib
#>
#> locale:
#> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
#>
#> attached base packages:
#> [1] stats graphics grDevices utils datasets methods base
#>
#> other attached packages:
#> [1] dplyr_0.8.0.1
#>
#> loaded via a namespace (and not attached):
#> [1] Rcpp_1.0.1 fansi_0.4.0 utf8_1.1.4 crayon_1.3.4
#> [5] digest_0.6.18 assertthat_0.2.1 R6_2.4.0 magrittr_1.5
#> [9] evaluate_0.13 pillar_1.3.1 cli_1.1.0 rlang_0.3.3
#> [13] stringi_1.4.3 rmarkdown_1.12 tools_3.5.3 stringr_1.4.0
#> [17] glue_1.3.1 purrr_0.3.2 xfun_0.5 yaml_2.2.0
#> [21] compiler_3.5.3 pkgconfig_2.0.2 htmltools_0.3.6 tidyselect_0.2.5
#> [25] knitr_1.22 tibble_2.1.1
salary %>%
mutate_at(.vars = vars("E", "M"), .funs = list(factor))
#> # A tibble: 6 x 4
#> S X E M
#> <dbl> <dbl> <fct> <fct>
#> 1 13876 1 1 1
#> 2 11608 1 3 0
#> 3 18701 1 3 1
#> 4 11283 1 2 0
#> 5 11767 1 3 0
#> 6 20872 2 2 1
salary %>%
mutate_at(.vars = vars("E", "M"), .funs = list(~factor(.)))
#> # A tibble: 6 x 4
#> S X E M
#> <dbl> <dbl> <fct> <fct>
#> 1 13876 1 1 1
#> 2 11608 1 3 0
#> 3 18701 1 3 1
#> 4 11283 1 2 0
#> 5 11767 1 3 0
#> 6 20872 2 2 1
salary%>%
在(.vars=vars(“E”,“M”),.funs=list(factor))处进行变异
#>#tibble:6 x 4
#>S X E M
#>
#> 1 13876 1 1 1
#> 2 11608 1 3 0
#> 3 18701 1 3 1
#> 4 11283 1 2 0
#> 5 11767 1 3 0
#> 6 20872 2 2 1
工资%>%
在(.vars=vars(“E”,“M”),.funs=list(~因子(.))处进行变异
#>#tibble:6 x 4
#>S X E M
#>
#> 1 13876 1 1 1
#> 2 11608 1 3 0
#> 3 18701 1 3 1
#> 4 11283 1 2 0
#> 5 11767 1 3 0
#> 6 20872 2 2 1
因为我问题中的示例只使用了一个函数,所以我认为不使用list
wrapper是最合理的选择
在实现多个函数时,可以选择上述两种语法中的一种。您是否尝试过
salary%>%mutate\u at(.vars=vars(“E”,“M”),factor)
?@MarianMinar谢谢!它也适用于3.6.0。但是你知道为什么在最新版本中,list(~factor)
会出现问题吗?我很高兴它能工作,因为我有较旧的版本,并且你的代码在我的设置中工作。我不知道确切的原因,但回想起来,我实际上不知道为什么list(~factor)
一开始就奏效了!这可能是一个rlang
的东西,但看起来他们捆绑了一个你的代码正在利用的bug。毕竟。对列应用函数已经生成了一个列表,因此如果我理解正确的话,list()
,是多余的。我把它作为一个感谢发布了!我不知道为什么funs()
或list()
以前可以工作,但仅仅使用不带包装器似乎更清楚。