不能将 psych::describe 与 dplyr 一起使用?
Can't use psych::describe with dplyr?
我注意到一个奇怪的错误,其中某些 dplyr 动词(在本例中为 select
)在与 psych::describe
结合使用时抛出错误,即使 mutate
不是:示例:
确定:
psych::describe(mtcars$mpg) %>%
mutate(variance = var(mtcars$mpg))
不行
psych::describe(mtcars$mpg) %>%
mutate(variance = var(mtcars$mpg)) %>%
select(n, mean, sd, variance, skew, kurtosis)
错误:
Error: Can't reconstruct data frame.
x The `[` method for class <psych/describe/data.frame> must return a data frame.
i It returned a <describe>.
我记得以前是这样的,或者是我想象的。 解决方法是什么?
请注意,我可以保存 describe(mtcars$mpg)
将其分配给一个对象 x
和 运行 class(x)
,我得到:
[1] "psych" "describe" "data.frame"
根据下面的评论,我意识到这种情况并非每次都会发生。这是我的 FRESH R 会话的会话信息,其中没有发生错误:
R version 4.0.0 (2020-04-24)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17763)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods
[7] base
other attached packages:
[1] forcats_0.5.0 stringr_1.4.0 dplyr_1.0.0
[4] purrr_0.3.4 readr_1.3.1 tidyr_1.1.0
[7] tibble_3.0.1 ggplot2_3.3.1 tidyverse_1.3.0
loaded via a namespace (and not attached):
[1] tidyselect_1.1.0 xfun_0.14 haven_2.3.1
[4] lattice_0.20-41 colorspace_1.4-1 vctrs_0.3.0
[7] generics_0.0.2 htmltools_0.4.0 yaml_2.2.1
[10] blob_1.2.1 rlang_0.4.6 pillar_1.4.4
[13] withr_2.2.0 glue_1.4.1 DBI_1.1.0
[16] dbplyr_1.4.4 modelr_0.1.8 readxl_1.3.1
[19] lifecycle_0.2.0 munsell_0.5.0 gtable_0.3.0
[22] cellranger_1.1.0 rvest_0.3.5 psych_1.9.12.31
[25] evaluate_0.14 knitr_1.28 parallel_4.0.0
[28] fansi_0.4.1 broom_0.5.6 Rcpp_1.0.4.6
[31] backports_1.1.7 scales_1.1.1 jsonlite_1.6.1
[34] tmvnsim_1.0-2 fs_1.4.1 mnormt_2.0.0
[37] hms_0.5.3 packrat_0.5.0 digest_0.6.25
[40] stringi_1.4.6 bookdown_0.19 grid_4.0.0
[43] cli_2.0.2 tools_4.0.0 magrittr_1.5
[46] crayon_1.3.4 pkgconfig_2.0.3 ellipsis_0.3.1
[49] xml2_1.3.2 reprex_0.3.0 lubridate_1.7.8
[52] assertthat_0.2.1 rmarkdown_2.2 httr_1.4.1
[55] rstudioapi_0.11 R6_2.4.1 nlme_3.1-147
[58] compiler_4.0.0
以下是确实发生的 R 会话的会话信息:
R version 4.0.0 (2020-04-24)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17763)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods
[7] base
other attached packages:
[1] janitor_2.0.1 gt_0.2.1 haven_2.3.1
[4] forcats_0.5.0 stringr_1.4.0 dplyr_1.0.0
[7] purrr_0.3.4 readr_1.3.1 tidyr_1.1.0
[10] tibble_3.0.1 ggplot2_3.3.1 tidyverse_1.3.0
loaded via a namespace (and not attached):
[1] colorspace_1.4-1 ellipsis_0.3.1
[3] rio_0.5.16 snakecase_0.11.0
[5] htmlTable_1.13.3 base64enc_0.1-3
[7] fs_1.4.1 rstudioapi_0.11
[9] mice_3.9.0 rstan_2.19.3
[11] fansi_0.4.1 lubridate_1.7.8
[13] xml2_1.3.2 splines_4.0.0
[15] mnormt_2.0.0 knitr_1.28
[17] Formula_1.2-3 jsonlite_1.6.1
[19] packrat_0.5.0 broom_0.5.6
[21] cluster_2.1.0 dbplyr_1.4.4
[23] png_0.1-7 compiler_4.0.0
[25] httr_1.4.1 backports_1.1.7
[27] assertthat_0.2.1 Matrix_1.2-18
[29] survey_4.0 cli_2.0.2
[31] prettyunits_1.1.1 acepack_1.4.1
[33] htmltools_0.4.0 tools_4.0.0
[35] gtable_0.3.0 glue_1.4.1
[37] descr_1.1.4 Rcpp_1.0.4.6
[39] carData_3.0-4 cellranger_1.1.0
[41] vctrs_0.3.0 gdata_2.18.0
[43] nlme_3.1-147 psych_1.9.12.31
[45] xfun_0.14 ps_1.3.3
[47] openxlsx_4.1.5 rvest_0.3.5
[49] lifecycle_0.2.0 weights_1.0.1
[51] gtools_3.8.2 scales_1.1.1
[53] ENmisc_1.2-7 hms_0.5.3
[55] parallel_4.0.0 inline_0.3.15
[57] RColorBrewer_1.1-2 yaml_2.2.1
[59] curl_4.3 gridExtra_2.3
[61] loo_2.2.0 StanHeaders_2.21.0-3
[63] rpart_4.1-15 latticeExtra_0.6-29
[65] stringi_1.4.6 plotrix_3.7-8
[67] checkmate_2.0.0 caTools_1.18.0
[69] pkgbuild_1.0.8 zip_2.0.4
[71] matrixStats_0.56.0 rlang_0.4.6
[73] pkgconfig_2.0.3 bitops_1.0-6
[75] evaluate_0.14 lattice_0.20-41
[77] htmlwidgets_1.5.1 tidyselect_1.1.0
[79] processx_3.4.2 plyr_1.8.6
[81] magrittr_1.5 bookdown_0.19
[83] R6_2.4.1 gplots_3.0.3
[85] generics_0.0.2 Hmisc_4.4-0
[87] DBI_1.1.0 pillar_1.4.4
[89] foreign_0.8-78 withr_2.2.0
[91] survival_3.1-12 abind_1.4-5
[93] nnet_7.3-13 modelr_0.1.8
[95] crayon_1.3.4 car_3.0-8
[97] KernSmooth_2.23-16 tmvnsim_1.0-2
[99] rmarkdown_2.2 jpeg_0.1-8.1
[101] grid_4.0.0 readxl_1.3.1
[103] data.table_1.12.8 blob_1.2.1
[105] callr_3.4.3 reprex_0.3.0
[107] digest_0.6.25 xtable_1.8-4
[109] poliscidata_2.2.3 stats4_4.0.0
[111] munsell_0.5.0 mitools_2.4
使用 dplyr
1.0.0,可以正常工作
library(dplyr)
psych::describe(mtcars$mpg) %>%
mutate(variance = var(mtcars$mpg)) %>%
select(n, mean, sd, variance, skew, kurtosis)
# n mean sd variance skew kurtosis
#1 32 20.09 6.03 36.32 0.61 -0.37
遇到了同样的问题。对我来说,添加 as.data.frame() %>%
解决了它:
psych::describe(mtcars$mpg) %>%
as.data.frame() %>%
mutate(variance = var(mtcars$mpg)) %>%
select(n, mean, sd, variance, skew, kurtosis)
# n mean sd variance skew kurtosis
# 1 32 20.09062 6.026948 36.3241 0.610655 -0.372766
sessionInfo()
R version 3.6.3 (2020-02-29)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.6
dplyr_1.0.0
tidyverse_1.3.0
psych_1.9.12.31
我注意到一个奇怪的错误,其中某些 dplyr 动词(在本例中为 select
)在与 psych::describe
结合使用时抛出错误,即使 mutate
不是:示例:
确定:
psych::describe(mtcars$mpg) %>%
mutate(variance = var(mtcars$mpg))
不行
psych::describe(mtcars$mpg) %>%
mutate(variance = var(mtcars$mpg)) %>%
select(n, mean, sd, variance, skew, kurtosis)
错误:
Error: Can't reconstruct data frame.
x The `[` method for class <psych/describe/data.frame> must return a data frame.
i It returned a <describe>.
我记得以前是这样的,或者是我想象的。 解决方法是什么?
请注意,我可以保存 describe(mtcars$mpg)
将其分配给一个对象 x
和 运行 class(x)
,我得到:
[1] "psych" "describe" "data.frame"
根据下面的评论,我意识到这种情况并非每次都会发生。这是我的 FRESH R 会话的会话信息,其中没有发生错误:
R version 4.0.0 (2020-04-24)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17763)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods
[7] base
other attached packages:
[1] forcats_0.5.0 stringr_1.4.0 dplyr_1.0.0
[4] purrr_0.3.4 readr_1.3.1 tidyr_1.1.0
[7] tibble_3.0.1 ggplot2_3.3.1 tidyverse_1.3.0
loaded via a namespace (and not attached):
[1] tidyselect_1.1.0 xfun_0.14 haven_2.3.1
[4] lattice_0.20-41 colorspace_1.4-1 vctrs_0.3.0
[7] generics_0.0.2 htmltools_0.4.0 yaml_2.2.1
[10] blob_1.2.1 rlang_0.4.6 pillar_1.4.4
[13] withr_2.2.0 glue_1.4.1 DBI_1.1.0
[16] dbplyr_1.4.4 modelr_0.1.8 readxl_1.3.1
[19] lifecycle_0.2.0 munsell_0.5.0 gtable_0.3.0
[22] cellranger_1.1.0 rvest_0.3.5 psych_1.9.12.31
[25] evaluate_0.14 knitr_1.28 parallel_4.0.0
[28] fansi_0.4.1 broom_0.5.6 Rcpp_1.0.4.6
[31] backports_1.1.7 scales_1.1.1 jsonlite_1.6.1
[34] tmvnsim_1.0-2 fs_1.4.1 mnormt_2.0.0
[37] hms_0.5.3 packrat_0.5.0 digest_0.6.25
[40] stringi_1.4.6 bookdown_0.19 grid_4.0.0
[43] cli_2.0.2 tools_4.0.0 magrittr_1.5
[46] crayon_1.3.4 pkgconfig_2.0.3 ellipsis_0.3.1
[49] xml2_1.3.2 reprex_0.3.0 lubridate_1.7.8
[52] assertthat_0.2.1 rmarkdown_2.2 httr_1.4.1
[55] rstudioapi_0.11 R6_2.4.1 nlme_3.1-147
[58] compiler_4.0.0
以下是确实发生的 R 会话的会话信息:
R version 4.0.0 (2020-04-24)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17763)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods
[7] base
other attached packages:
[1] janitor_2.0.1 gt_0.2.1 haven_2.3.1
[4] forcats_0.5.0 stringr_1.4.0 dplyr_1.0.0
[7] purrr_0.3.4 readr_1.3.1 tidyr_1.1.0
[10] tibble_3.0.1 ggplot2_3.3.1 tidyverse_1.3.0
loaded via a namespace (and not attached):
[1] colorspace_1.4-1 ellipsis_0.3.1
[3] rio_0.5.16 snakecase_0.11.0
[5] htmlTable_1.13.3 base64enc_0.1-3
[7] fs_1.4.1 rstudioapi_0.11
[9] mice_3.9.0 rstan_2.19.3
[11] fansi_0.4.1 lubridate_1.7.8
[13] xml2_1.3.2 splines_4.0.0
[15] mnormt_2.0.0 knitr_1.28
[17] Formula_1.2-3 jsonlite_1.6.1
[19] packrat_0.5.0 broom_0.5.6
[21] cluster_2.1.0 dbplyr_1.4.4
[23] png_0.1-7 compiler_4.0.0
[25] httr_1.4.1 backports_1.1.7
[27] assertthat_0.2.1 Matrix_1.2-18
[29] survey_4.0 cli_2.0.2
[31] prettyunits_1.1.1 acepack_1.4.1
[33] htmltools_0.4.0 tools_4.0.0
[35] gtable_0.3.0 glue_1.4.1
[37] descr_1.1.4 Rcpp_1.0.4.6
[39] carData_3.0-4 cellranger_1.1.0
[41] vctrs_0.3.0 gdata_2.18.0
[43] nlme_3.1-147 psych_1.9.12.31
[45] xfun_0.14 ps_1.3.3
[47] openxlsx_4.1.5 rvest_0.3.5
[49] lifecycle_0.2.0 weights_1.0.1
[51] gtools_3.8.2 scales_1.1.1
[53] ENmisc_1.2-7 hms_0.5.3
[55] parallel_4.0.0 inline_0.3.15
[57] RColorBrewer_1.1-2 yaml_2.2.1
[59] curl_4.3 gridExtra_2.3
[61] loo_2.2.0 StanHeaders_2.21.0-3
[63] rpart_4.1-15 latticeExtra_0.6-29
[65] stringi_1.4.6 plotrix_3.7-8
[67] checkmate_2.0.0 caTools_1.18.0
[69] pkgbuild_1.0.8 zip_2.0.4
[71] matrixStats_0.56.0 rlang_0.4.6
[73] pkgconfig_2.0.3 bitops_1.0-6
[75] evaluate_0.14 lattice_0.20-41
[77] htmlwidgets_1.5.1 tidyselect_1.1.0
[79] processx_3.4.2 plyr_1.8.6
[81] magrittr_1.5 bookdown_0.19
[83] R6_2.4.1 gplots_3.0.3
[85] generics_0.0.2 Hmisc_4.4-0
[87] DBI_1.1.0 pillar_1.4.4
[89] foreign_0.8-78 withr_2.2.0
[91] survival_3.1-12 abind_1.4-5
[93] nnet_7.3-13 modelr_0.1.8
[95] crayon_1.3.4 car_3.0-8
[97] KernSmooth_2.23-16 tmvnsim_1.0-2
[99] rmarkdown_2.2 jpeg_0.1-8.1
[101] grid_4.0.0 readxl_1.3.1
[103] data.table_1.12.8 blob_1.2.1
[105] callr_3.4.3 reprex_0.3.0
[107] digest_0.6.25 xtable_1.8-4
[109] poliscidata_2.2.3 stats4_4.0.0
[111] munsell_0.5.0 mitools_2.4
使用 dplyr
1.0.0,可以正常工作
library(dplyr)
psych::describe(mtcars$mpg) %>%
mutate(variance = var(mtcars$mpg)) %>%
select(n, mean, sd, variance, skew, kurtosis)
# n mean sd variance skew kurtosis
#1 32 20.09 6.03 36.32 0.61 -0.37
遇到了同样的问题。对我来说,添加 as.data.frame() %>%
解决了它:
psych::describe(mtcars$mpg) %>%
as.data.frame() %>%
mutate(variance = var(mtcars$mpg)) %>%
select(n, mean, sd, variance, skew, kurtosis)
# n mean sd variance skew kurtosis
# 1 32 20.09062 6.026948 36.3241 0.610655 -0.372766
sessionInfo()
R version 3.6.3 (2020-02-29)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.6
dplyr_1.0.0
tidyverse_1.3.0
psych_1.9.12.31