dplyr中的exp函数mutate返回1';s
我试图使用指数函数转换变量,但是我获得了dplyr中的exp函数mutate返回1';s,r,R,我试图使用指数函数转换变量,但是我获得了1的值 dat %>% mutate(estimate_exp = exp(estimate)) 其中: # A tibble: 30 x 3 # Groups: .id [30] .id estimate estimate_exp <chr> <dbl> <dbl> 1 COP 0.000363 1.00 2 D 0.0001
1
的值
dat %>%
mutate(estimate_exp = exp(estimate))
其中:
# A tibble: 30 x 3
# Groups: .id [30]
.id estimate estimate_exp
<chr> <dbl> <dbl>
1 COP 0.000363 1.00
2 D 0.000128 1.00
3 MKC 0.000661 1.00
4 XRAY -0.000220 1.00
5 AZO 0.000646 1.00
[1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
编辑:
我重新启动了R会话,现在得到以下输出:
当我跑步时:
models %>%
unnest(tidymodels) %>%
mutate(estimate_exp = exp(estimate)) %>%
select(estimate_exp)
# A tibble: 60 x 2
# Groups: .id [30]
.id estimate_exp
<chr> <dbl>
1 LEN 40.1
2 LEN 1.00
3 USB 8.47
4 USB 1.00
5 KMI 2.64
6 KMI 1.00
7 CSX 0.00592
8 CSX 1.00
9 AMT 0.000845
10 AMT 1.00
models2 %>%
unnest(tidymodels) %>%
filter(term == "date") %>%
mutate(estimate_exp = exp(estimate)) %>%
select(estimate_exp)
返回:
# A tibble: 30 x 2
# Groups: .id [30]
.id estimate_exp
<chr> <dbl>
1 LEN 1.00
2 USB 1.00
3 KMI 1.00
4 CSX 1.00
5 AMT 1.00
6 VRSK 1.00
7 CTSH 1.00
这给了我输出:
Adding missing grouping variables: `.id`
# A tibble: 30 x 2
# Groups: .id [30]
.id estimate_exp
<chr> <dbl>
1 AES 1.00
2 AMT 1.00
3 ANET 1.00
4 BSX 1.00
5 CAH 0.999
6 CSX 1.00
编辑:通过刷新R会话,我运行以下操作:
library(dplyr)
library(broom)
library(purrr)
models2 <- d %>%
group_by(.id) %>%
nest() %>%
mutate(models = map(data, ~lm(logPrice ~ date, data = .x))) %>%
mutate(
tidymodels = map(models, ~tidy(.x)),
glancemodels = map(models, ~glance(.x)),
augmentmodels = map(models, ~augment(.x))
)
mod1 <- models2 %>% unnest(tidymodels) %>% filter(term == "date")
exp(mod1$estimate)
会话信息:
R version 3.6.1 (2019-07-05)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.3 LTS
Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1
locale:
[1] LC_CTYPE=C.UTF-8 LC_NUMERIC=C LC_TIME=C.UTF-8
[4] LC_COLLATE=C.UTF-8 LC_MONETARY=C.UTF-8 LC_MESSAGES=C.UTF-8
[7] LC_PAPER=C.UTF-8 LC_NAME=C LC_ADDRESS=C
[10] LC_TELEPHONE=C LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] grid stats graphics grDevices utils datasets methods base
other attached packages:
[1] rvest_0.3.5 xml2_1.2.2 tibbletime_0.1.3
[4] tsfeatures_1.0.1 gsubfn_0.7 proto_1.0.0
[7] ggmap_3.0.0 ggrepel_0.8.1 directlabels_2018.05.22
[10] patchwork_1.0.0 cowplot_1.0.0 gtable_0.3.0
[13] stargazer_5.2.2 e1071_1.7-3 lightgbm_2.3.2
[16] R6_2.4.1 tensorflow_2.0.0 keras_2.2.5.0
[19] drlib_0.1.1 PRROC_1.3.1 pROC_1.16.1
[22] caret_6.0-85 lattice_0.20-38 Matrix_1.2-17
[25] xgboostExplainer_0.1 broom_0.5.3 imputeTS_3.0
[28] data.table_1.12.8 viridis_0.5.1 viridisLite_0.3.0
[31] CVXR_0.99-7 timetk_0.1.2 forcats_0.4.0
[34] readr_1.3.1 tibble_2.99.99.9014 tidyverse_1.3.0
[37] tsibble_0.8.5 rsample_0.0.5.9000 kableExtra_1.1.0
[40] knitr_1.27 xgboost_0.90.0.2 reticulate_1.14-9001
[43] formatR_1.7 tidyquant_0.5.9 quantmod_0.4-15
[46] TTR_0.23-6 PerformanceAnalytics_1.5.3 xts_0.12-0
[49] zoo_1.8-7 ggplot2_3.3.0.9000 purrr_0.3.3
[52] lubridate_1.7.4 tidyr_1.0.2 future.apply_1.4.0
[55] furrr_0.1.0 future_1.16.0 stringr_1.4.0
[58] stringi_1.4.5 dplyr_0.8.99.9000
loaded via a namespace (and not attached):
[1] readxl_1.3.1 backports_1.1.5 scs_1.3-2
[4] selectr_0.4-2 plyr_1.8.5 splines_3.6.1
[7] gmp_0.5-13.6 listenv_0.8.0 tfruns_1.4
[10] digest_0.6.23 foreach_1.4.7 htmltools_0.4.0
[13] fansi_0.4.1 magrittr_1.5 recipes_0.1.9
[16] globals_0.12.5 modelr_0.1.5 gower_0.2.1
[19] R.utils_2.9.2 anytime_0.3.7 forecast_8.10
[22] tseries_0.10-47 jpeg_0.1-8.1 waterfalls_0.1.2
[25] colorspace_1.4-1 lobstr_1.1.1 haven_2.2.0
[28] xfun_0.12 tcltk_3.6.1 crayon_1.3.4
[31] jsonlite_1.6 zeallot_0.1.0 survival_2.44-1.1
[34] iterators_1.0.12 glue_1.3.1 ipred_0.9-9
[37] webshot_0.5.2 Quandl_2.10.0 ECOSolveR_0.5.3
[40] Rmpfr_0.8-1 scales_1.1.0 stinepack_1.4
[43] DBI_1.1.0 Rcpp_1.0.3 bit_1.1-15.1
[46] stats4_3.6.1 lava_1.6.6 prodlim_2019.11.13
[49] httr_1.4.1 ellipsis_0.3.0 farver_2.0.3
[52] pkgconfig_2.0.3 R.methodsS3_1.7.1 nnet_7.3-12
[55] dbplyr_1.4.2 utf8_1.1.4 labeling_0.3
[58] tidyselect_0.2.99.9000 rlang_0.4.4.9000 reshape2_1.4.3
[61] munsell_0.5.0 cellranger_1.1.0 tools_3.6.1
[64] cli_2.0.1 generics_0.0.2 evaluate_0.14
[67] ModelMetrics_1.2.2.1 bit64_0.9-7 fs_1.3.1
[70] RgoogleMaps_1.4.5.2 nlme_3.1-142 whisker_0.4
[73] R.oo_1.23.0 compiler_3.6.1 rstudioapi_0.10
[76] png_0.1-7 curl_4.3 testthat_2.3.1
[79] reprex_0.3.0 desc_1.2.0 urca_1.3-0
[82] vctrs_0.2.99.9005 pillar_1.4.3.9000 lifecycle_0.1.0
[85] lmtest_0.9-37 bitops_1.0-6 gridExtra_2.3
[88] codetools_0.2-16 pkgload_1.0.2 MASS_7.3-51.4
[91] assertthat_0.2.1 rprojroot_1.3-2 rjson_0.2.20
[94] withr_2.1.2 fracdiff_1.5-1 parallel_3.6.1
[97] hms_0.5.3 quadprog_1.5-8 rpart_4.1-15
[100] timeDate_3043.102 class_7.3-15 rmarkdown_2.1
[103] base64enc_0.1-3
R version 3.6.1 (2019-07-05)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.3 LTS
Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1
locale:
[1] LC_CTYPE=C.UTF-8 LC_NUMERIC=C LC_TIME=C.UTF-8
[4] LC_COLLATE=C.UTF-8 LC_MONETARY=C.UTF-8 LC_MESSAGES=C.UTF-8
[7] LC_PAPER=C.UTF-8 LC_NAME=C LC_ADDRESS=C
[10] LC_TELEPHONE=C LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] grid stats graphics grDevices utils datasets methods base
other attached packages:
[1] gsubfn_0.7 proto_1.0.0 ggmap_3.0.0
[4] ggrepel_0.8.1 directlabels_2018.05.22 patchwork_1.0.0
[7] cowplot_1.0.0 gtable_0.3.0 stargazer_5.2.2
[10] e1071_1.7-3 lightgbm_2.3.2 R6_2.4.1
[13] tensorflow_2.0.0 keras_2.2.5.0 drlib_0.1.1
[16] PRROC_1.3.1 pROC_1.16.1 caret_6.0-85
[19] lattice_0.20-38 Matrix_1.2-17 xgboostExplainer_0.1
[22] broom_0.5.3 imputeTS_3.0 data.table_1.12.8
[25] viridis_0.5.1 viridisLite_0.3.0 CVXR_0.99-7
[28] timetk_0.1.2 forcats_0.4.0 readr_1.3.1
[31] tibble_2.99.99.9014 tidyverse_1.3.0 tsibble_0.8.5
[34] rsample_0.0.5.9000 kableExtra_1.1.0 knitr_1.27
[37] xgboost_0.90.0.2 reticulate_1.14-9001 formatR_1.7
[40] tidyquant_0.5.9 quantmod_0.4-15 TTR_0.23-6
[43] PerformanceAnalytics_1.5.3 xts_0.12-0 zoo_1.8-7
[46] ggplot2_3.3.0.9000 purrr_0.3.3 lubridate_1.7.4
[49] tidyr_1.0.2 future.apply_1.4.0 furrr_0.1.0
[52] future_1.16.0 stringr_1.4.0 stringi_1.4.5
[55] dplyr_0.8.99.9000
loaded via a namespace (and not attached):
[1] readxl_1.3.1 backports_1.1.5 scs_1.3-2
[4] plyr_1.8.5 splines_3.6.1 gmp_0.5-13.6
[7] listenv_0.8.0 tfruns_1.4 digest_0.6.23
[10] foreach_1.4.7 htmltools_0.4.0 fansi_0.4.1
[13] magrittr_1.5 recipes_0.1.9 globals_0.12.5
[16] modelr_0.1.5 gower_0.2.1 R.utils_2.9.2
[19] anytime_0.3.7 forecast_8.10 tseries_0.10-47
[22] jpeg_0.1-8.1 waterfalls_0.1.2 colorspace_1.4-1
[25] rvest_0.3.5 lobstr_1.1.1 haven_2.2.0
[28] xfun_0.12 tcltk_3.6.1 crayon_1.3.4
[31] jsonlite_1.6 zeallot_0.1.0 survival_2.44-1.1
[34] iterators_1.0.12 glue_1.3.1 ipred_0.9-9
[37] webshot_0.5.2 Quandl_2.10.0 ECOSolveR_0.5.3
[40] Rmpfr_0.8-1 scales_1.1.0 stinepack_1.4
[43] DBI_1.1.0 Rcpp_1.0.3 bit_1.1-15.1
[46] stats4_3.6.1 lava_1.6.6 prodlim_2019.11.13
[49] httr_1.4.1 ellipsis_0.3.0 pkgconfig_2.0.3
[52] R.methodsS3_1.7.1 nnet_7.3-12 dbplyr_1.4.2
[55] utf8_1.1.4 tidyselect_0.2.99.9000 rlang_0.4.4.9000
[58] reshape2_1.4.3 munsell_0.5.0 cellranger_1.1.0
[61] tools_3.6.1 cli_2.0.1 generics_0.0.2
[64] evaluate_0.14 ModelMetrics_1.2.2.1 bit64_0.9-7
[67] fs_1.3.1 RgoogleMaps_1.4.5.2 nlme_3.1-142
[70] whisker_0.4 R.oo_1.23.0 xml2_1.2.2
[73] compiler_3.6.1 rstudioapi_0.10 png_0.1-7
[76] curl_4.3 reprex_0.3.0 urca_1.3-0
[79] vctrs_0.2.99.9005 pillar_1.4.3.9000 lifecycle_0.1.0
[82] lmtest_0.9-37 bitops_1.0-6 gridExtra_2.3
[85] codetools_0.2-16 MASS_7.3-51.4 assertthat_0.2.1
[88] rjson_0.2.20 withr_2.1.2 fracdiff_1.5-1
[91] parallel_3.6.1 hms_0.5.3 quadprog_1.5-8
[94] rpart_4.1-15 timeDate_3043.102 class_7.3-15
[97] rmarkdown_2.1 base64enc_0.1-3
没有什么不对的。如果我们提取列中的值,它将给出完整值,而不是
print
格式化的tbl_df列值
library(dplyr)
dat %>%
mutate(estimate_exp = exp(estimate)) %>%
pull(estimate_exp)
#[1] 1.0003633 1.0001284 1.0006611 0.9997798 1.0006460 1.0003745 1.0004764 1.0002343 1.0002678 0.9998316
#[11] 0.9999734 1.0006664 1.0005484 1.0003239 0.9998811 0.9999193 1.0002518 0.9999877 1.0006545 1.0003430
#[21] 1.0005299 1.0004024 1.0004834 1.0008319 1.0000060 1.0003500 1.0003589 1.0001489 1.0004167 1.0003802
此外,我们不需要按属性分组
dat %>%
ungroup %>%
mutate(estimate_exp = exp(estimate)) %>%
pull(estimate_exp)
#[1] 1.0003633 1.0001284 1.0006611 0.9997798 1.0006460 1.0003745 1.0004764 1.0002343 1.0002678 0.9998316
#[11] 0.9999734 1.0006664 1.0005484 1.0003239 0.9998811 0.9999193 1.0002518 0.9999877 1.0006545 1.0003430
#[21] 1.0005299 1.0004024 1.0004834 1.0008319 1.0000060 1.0003500 1.0003589 1.0001489 1.0004167 1.0003802
使现代化
使用新数据“d”
models2 %>%
unnest(tidymodels) %>%
ungroup %>%
filter(term == "date") %>%
mutate(estimate_exp = exp(estimate)) %>%
select(estimate_exp) %>% pull(estimate_exp)
#[1] 1.0006405 1.0007140 0.9999498 1.0003551 0.9994210 1.0004718 0.9997622 1.0003188 1.0001203 1.0003742
#[11] 1.0001127 1.0005580 1.0000700 0.9990263 0.9997404 1.0001405 1.0000650 1.0002640 1.0004100 1.0009941
#[21] 1.0000995 1.0006812 1.0009634 1.0002974 0.9998595 0.9999282 1.0006958 1.0004811 1.0005498 1.0004972
没有什么不对的。如果我们提取列中的值,它将给出完整值,而不是
print
格式化的tbl_df列值
library(dplyr)
dat %>%
mutate(estimate_exp = exp(estimate)) %>%
pull(estimate_exp)
#[1] 1.0003633 1.0001284 1.0006611 0.9997798 1.0006460 1.0003745 1.0004764 1.0002343 1.0002678 0.9998316
#[11] 0.9999734 1.0006664 1.0005484 1.0003239 0.9998811 0.9999193 1.0002518 0.9999877 1.0006545 1.0003430
#[21] 1.0005299 1.0004024 1.0004834 1.0008319 1.0000060 1.0003500 1.0003589 1.0001489 1.0004167 1.0003802
此外,我们不需要按属性分组
dat %>%
ungroup %>%
mutate(estimate_exp = exp(estimate)) %>%
pull(estimate_exp)
#[1] 1.0003633 1.0001284 1.0006611 0.9997798 1.0006460 1.0003745 1.0004764 1.0002343 1.0002678 0.9998316
#[11] 0.9999734 1.0006664 1.0005484 1.0003239 0.9998811 0.9999193 1.0002518 0.9999877 1.0006545 1.0003430
#[21] 1.0005299 1.0004024 1.0004834 1.0008319 1.0000060 1.0003500 1.0003589 1.0001489 1.0004167 1.0003802
使现代化
使用新数据“d”
models2 %>%
unnest(tidymodels) %>%
ungroup %>%
filter(term == "date") %>%
mutate(estimate_exp = exp(estimate)) %>%
select(estimate_exp) %>% pull(estimate_exp)
#[1] 1.0006405 1.0007140 0.9999498 1.0003551 0.9994210 1.0004718 0.9997622 1.0003188 1.0001203 1.0003742
#[11] 1.0001127 1.0005580 1.0000700 0.9990263 0.9997404 1.0001405 1.0000650 1.0002640 1.0004100 1.0009941
#[21] 1.0000995 1.0006812 1.0009634 1.0002974 0.9998595 0.9999282 1.0006958 1.0004811 1.0005498 1.0004972
哦,我明白了!这是有道理的。如何使输出具有更多的小数位数?当我应用你的代码时,我得到:
11111
我有选项(scipen=999)
@user113156是因为软件包版本不同,我无法复制吗that@user113156有一个grup_by属性,您也可以删除它dat%>%ungroup%>%mutate(estimate_exp=exp(估计值))%%>%pull(估计值)
使用dat%%>%ungroup(.id)%%>%mutate(估计值=exp(估计值))%%>%pull(估计值)
仍然会给我1…
输出。我也在使用这个版本。可能是因为dplyr
,我不知道。但是添加数据。帧
解决了我的问题。再次感谢!哦,我明白了!这很有意义。我如何使我的输出有更多的小数位?当我应用你的代码时,我得到:1 11 11 11 11 11
我有选项(scipen=999)
@user113156是因为软件包版本不同,我无法复制吗that@user113156有一个grup_by属性,您也可以删除它dat%>%ungroup%>%mutate(估计值=exp(估计值))%>%pull(估计值)
使用dat%%>%ungroup(.id)%%>%mutate(estimate\u exp=exp(estimate))%%>%pull(estimate\u exp)
仍然会给我1…
输出。我也在使用这个版本。也许这与dplyr有关,我不知道。但是添加数据。框架解决了我的问题。再次感谢!我也有相同的包版本packageVersion('dplyr')#[1]“0.8.99.9000”
关于R 3.6.2
你能检查exp(dat$estimate)的输出吗
即,我们直接提取列并获取exp
当我应用过滤器时出错-我添加了一个编辑。由于您加载了很多包,是否有可能mutate
或exp
被另一个包中的相同函数屏蔽了我现在必须离开。稍后还会检查吗在R3.6.2
上有相同的软件包版本packageVersion('dplyr')#[1]“0.8.99.9000”
您能检查exp(dat$estimate)的输出吗
即,我们直接提取列并获取exp
当我应用过滤器时出错-我添加了一个编辑。由于您加载了很多包,是否有可能mutate
或exp
被另一个包中的相同函数屏蔽我现在必须离开。稍后将检查
dat %>%
ungroup %>%
mutate(estimate_exp = exp(estimate)) %>%
pull(estimate_exp)
#[1] 1.0003633 1.0001284 1.0006611 0.9997798 1.0006460 1.0003745 1.0004764 1.0002343 1.0002678 0.9998316
#[11] 0.9999734 1.0006664 1.0005484 1.0003239 0.9998811 0.9999193 1.0002518 0.9999877 1.0006545 1.0003430
#[21] 1.0005299 1.0004024 1.0004834 1.0008319 1.0000060 1.0003500 1.0003589 1.0001489 1.0004167 1.0003802
models2 %>%
unnest(tidymodels) %>%
ungroup %>%
filter(term == "date") %>%
mutate(estimate_exp = exp(estimate)) %>%
select(estimate_exp) %>% pull(estimate_exp)
#[1] 1.0006405 1.0007140 0.9999498 1.0003551 0.9994210 1.0004718 0.9997622 1.0003188 1.0001203 1.0003742
#[11] 1.0001127 1.0005580 1.0000700 0.9990263 0.9997404 1.0001405 1.0000650 1.0002640 1.0004100 1.0009941
#[21] 1.0000995 1.0006812 1.0009634 1.0002974 0.9998595 0.9999282 1.0006958 1.0004811 1.0005498 1.0004972