tibble::tribble(
+ ~Sepal.Length, ~Sepal.Width, ~Petal.Length, ~Petal.Width, ~Species,
+ 5.1, 3.5, 1.4, 0.2, "setosa",
+ 4.9, 3, 1.4, 0.2, "setosa",
+ 4.7, 3.2, 1.3, 0.2, "setosa",
+ 4.6, 3.1, 1.5, 0.2, "setosa",
+ 5, 3.6, 1.4, 0.2, "setosa",
+ 5.4, 3.9, 1.7, 0.4, "setosa"
+ ) %>%
+ mutate(row_sum = rowSums(select(., 1:4))) %>%
+ mutate_at(1:4, ~ ./row_sum)
Error in mutate_impl(.data, dots) :
Column `Sepal.Length` must be length 6 (the number of rows) or one, not 150
I just try the mutate_at
function on the Stack Overflow example, but I get this weird err info.
Obviously, the rows of th table is 6 instead of 150.
And I try one more example, and get another err info. I feel the combination mutate_at(m:n, ~ ./...)
doesn't works always.
> tibble::tribble(
+ ~year, ~month, ~reg_cnt, ~m0_deal, ~m1_deal, ~m2_deal, ~m3_deal, ~m4_deal, ~m5_deal, ~m0_amount, ~m1_amount, ~m2_amount, ~m3_amount, ~m4_amount, ~m5_amount,
+ 2018L, 1L, 16L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
+ 2018L, 2L, 35331L, 441L, NA, NA, 151L, 1L, NA, 1021541.95011338, NA, NA, 923841.059602649, 5e+05, NA,
+ 2018L, 3L, 7554L, 77L, NA, 8L, 21L, NA, NA, 993506.493506494, NA, 937500, 976190.476190476, NA, NA,
+ 2018L, 4L, 2211L, NA, 16L, NA, NA, NA, NA, NA, 968750, NA, NA, NA, NA,
+ 2018L, 5L, 11070L, 1024L, 1L, NA, NA, NA, NA, 1025390.625, 5e+05, NA, NA, NA, NA,
+ 2018L, 6L, 11665L, 1107L, NA, NA, NA, NA, NA, 1001355.01355014, NA, NA, NA, NA, NA
+ ) %>%
+
+ mutate_all(~ ifelse(is.na(.),0,.)) %>%
+ mutate_at(vars(m0_deal:m5_deal), ~ ./reg_cnt)
Error in mutate_impl(.data, dots) :
Evaluation error: non-numeric argument to binary operator.
Here is my session info.
> sessionInfo()
R version 3.4.4 (2018-03-15)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
Matrix products: default
locale:
[1] LC_COLLATE=Chinese (Simplified)_People's Republic of China.936 LC_CTYPE=Chinese (Simplified)_People's Republic of China.936
[3] LC_MONETARY=Chinese (Simplified)_People's Republic of China.936 LC_NUMERIC=C
[5] LC_TIME=Chinese (Simplified)_People's Republic of China.936
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] bindrcpp_0.2.2 DT_0.4 formattable_0.2.0.1 readxl_1.1.0 pryr_0.1.4 skimr_1.0.2 caret_6.0-79 lattice_0.20-35
[9] knitr_1.20 lubridate_1.7.4 forcats_0.3.0 stringr_1.3.0 dplyr_0.7.4 purrr_0.2.4 readr_1.1.1 tidyr_0.8.0
[17] tibble_1.4.2 ggplot2_2.2.1 tidyverse_1.2.1 RODBC_1.3-15
loaded via a namespace (and not attached):
[1] nlme_3.1-137 dimRed_0.1.0 httr_1.3.1 rprojroot_1.3-2 tools_3.4.4 backports_1.1.2 R6_2.2.2 rpart_4.1-13
[9] lazyeval_0.2.1 colorspace_1.3-2 nnet_7.3-12 withr_2.1.2 tidyselect_0.2.4 mnormt_1.5-5 compiler_3.4.4 cli_1.0.0
[17] rvest_0.3.2 xml2_1.2.0 scales_0.5.0 sfsmisc_1.1-2 DEoptimR_1.0-8 psych_1.8.4 robustbase_0.93-0 digest_0.6.15
[25] foreign_0.8-70 rmarkdown_1.9 pkgconfig_2.0.1 htmltools_0.3.6 htmlwidgets_1.2 rlang_0.2.0 ddalpha_1.3.3 rstudioapi_0.7
[33] bindr_0.1.1 jsonlite_1.5 ModelMetrics_1.1.0 magrittr_1.5 Matrix_1.2-14 Rcpp_0.12.17 munsell_0.4.3 abind_1.4-5
[41] stringi_1.1.7 MASS_7.3-50 plyr_1.8.4 recipes_0.1.2 grid_3.4.4 parallel_3.4.4 crayon_1.3.4 haven_1.1.1
[49] splines_3.4.4 pander_0.6.1 hms_0.4.2 pillar_1.2.2 reshape2_1.4.3 codetools_0.2-15 stats4_3.4.4 CVST_0.2-1
[57] magic_1.5-8 glue_1.2.0 evaluate_0.10.1 modelr_0.1.2 foreach_1.4.4 cellranger_1.1.0 gtable_0.2.0 kernlab_0.9-26
[65] assertthat_0.2.0 DRR_0.0.3 gower_0.1.2 prodlim_2018.04.18 broom_0.4.4 class_7.3-14 survival_2.42-3 geometry_0.3-6
[73] timeDate_3043.102 RcppRoll_0.2.2 iterators_1.0.9 lava_1.6.1 ipred_0.9-6