【发布时间】:2018-08-29 18:33:01
【问题描述】:
我正在使用 r 3.3.3、dplyr 0.7.4 和 Hmisc 4.1-1。我注意到我加载包的顺序会影响 dplyr::summaries 函数是否有效。我知道以不同的顺序加载包会掩盖某些功能,但我使用 package::function() 语法来避免这个问题。确切的问题围绕着标记变量。我知道过去 tidyverse 和变量标签存在问题,但似乎没有人能解决为什么会发生这种特殊情况。
第一个有效的例子 - 我只加载 Hmisc 然后 dplyr 并且我能够总结数据 -
#this works fine
library(Hmisc)
library(dplyr)
Hmisc::label(iris$Petal.Width) <- "Petal Width"
sumpct <- iris %>%
dplyr::group_by(Species) %>%
dplyr::summarise(med =median(Petal.Width),A40 = round(100*ecdf(Petal.Width)(.40),1),
A50 =round(100*ecdf(Petal.Width)(.50),1),
mns = mean(Petal.Width),
lowermean = mean(Petal.Width)-sd(Petal.Width),
lowermedian = median(Petal.Width) - sd(Petal.Width))
下面的第二个示例中断。我开始一个新会话并在 Hmisc 之后加载 tidyverse 并仍然使用 package::function() 语法,但这会引发错误:
summarise_impl(.data, dots) 中的错误:评估错误:
x和labels必须是同一类型。
第二个例子:
###restart session
#this example does not work
library(Hmisc)
library(tidyverse)
Hmisc::label(iris$Petal.Width) <- "Petal Width"
sumpct <- iris %>%
dplyr::group_by(Species) %>%
dplyr::summarise(med =median(Petal.Width),A40 = round(100*ecdf(Petal.Width)(.40),1),
A50 =round(100*ecdf(Petal.Width)(.50),1),
mns = mean(Petal.Width),
lowermean = mean(Petal.Width)-sd(Petal.Width),
lowermedian = median(Petal.Width) - sd(Petal.Width))
但是,第三个示例确实有效,我只是在 Hmisc 之前重新启动会话并加载 tidyverse
第三个例子:
###switch order of loading packages and this works
library(tidyverse)
library(Hmisc)
Hmisc::label(iris$Petal.Width) <- "Petal Width"
sumpct <- iris %>%
dplyr::group_by(Species) %>%
dplyr::summarise(med =median(Petal.Width),A40 = round(100*ecdf(Petal.Width)(.40),1),
A50 =round(100*ecdf(Petal.Width)(.50),1),
mns = mean(Petal.Width),
lowermean = mean(Petal.Width)-sd(Petal.Width),
lowermedian = median(Petal.Width) - sd(Petal.Width))
所以我的问题是为什么我在使用 package::function() 语法时加载包的顺序很重要,特别是关于标记变量和 tidyverse?
更新:错误的会话信息如下:
sessionInfo()
R 版本 3.3.3 (2017-03-06) 运行条件:Windows 7 x64 附加的基础包:[1] stats graphics grDevices utils datasets methods base
其他附加包:[1] bindrcpp_0.2 forcats_0.3.0
stringr_1.3.0 dplyr_0.7.4 [5] purrr_0.2.4 readr_1.1.1
tidyr_0.8.0 tibble_1.4.2 [9] tidyverse_1.2.1 Hmisc_4.1-1
ggplot2_2.2.1 Formula_1.2-2 [13]survival_2.41-3 lattice_0.20-35通过命名空间加载(未附加):[1] reshape2_1.4.3
splines_3.3.3 Have_1.1.1 [4] colorspace_1.3-2
htmltools_0.3.6 base64enc_0.1-3 [7] rlang_0.2.0
柱子_1.2.1 国外_0.8-69 [10] 胶水_1.2.0
RColorBrewer_1.1-2 readxl_1.0.0 [13] modelr_0.1.1
plyr_1.8.4 bindr_0.1.1 [16] cellranger_1.1.0
munsell_0.4.3 gtable_0.2.0 [19] rvest_0.3.2
htmlwidgets_1.0 psych_1.7.8 [22] latticeExtra_0.6-28 knitr_1.20 parallel_3.3.3 [25] htmlTable_1.11.2
broom_0.4.3 Rcpp_0.12.16 [28] acepack_1.4.1
scales_0.5.0 backports_1.1.2 [31] checkmate_1.8.5
jsonlite_1.5 gridExtra_2.3 [34] mnormt_1.5-5
hms_0.4.2 digest_0.6.15 [37] stringi_1.1.7
grid_3.3.3 cli_1.0.0 [40] 工具_3.3.3
magrittr_1.5lazyeval_0.2.1 [43] cluster_2.0.6
crayon_1.3.4 pkgconfig_2.0.1 [46] Matrix_1.2-12
xml2_1.2.0 data.table_1.10.4-3 [49] lubridate_1.7.3
assertthat_0.2.0 httr_1.3.1 [52] rstudioapi_0.7
R6_2.2.2 rpart_4.1-13 [55] nnet_7.3-12
nlme_3.1-131.1
【问题讨论】:
-
我刚试过这个,第二个和第三个选项都给了我错误。那么也许是与 Hmisc 和 tidyverse 的组合有关,而不是包装顺序?令人费解
-
有趣 我刚刚再次尝试了第三个示例,它成功了,您使用的是什么版本,您是否重新启动了会话?不管怎样,这都是一个有趣的问题。
-
我目前有 Hmisc 4.1-1、dplyr 0.74、tidyverse 1.2.1、R 3.4.3。我将尝试更新 R 和所有其他软件包以查看会发生什么,建议您也这样做并发布您的会话信息
-
刚刚添加的会话信息
-
只是为了深入研究这个问题,一个最小的例子是在你将标签分配给
iris$Petal.Width之后执行head(iris)。