dplyr::group_by 似乎在 r 中删除了 pander 选项
dplyr::group_by appears to drop pander options in r
我不确定这是错误还是我做错了什么,但是当我在在将数据框输入 pander 之前。我在 Rstudio Notebook 中工作。示例如下:
加载库并设置选项:
library(pander)
panderOptions('round', 2)
panderOptions('keep.trailing.zeros', TRUE)
panderOptions('table.split.table', Inf)
library(tidyverse)
创建一些数据:
set.seed(10)
df <- data.frame(x = rnorm(10), y = rnorm(10), class = c("a", "b"))
print(df)
x y class
0.01874617 1.10177950 a
-0.18425254 0.75578151 b
-1.37133055 -0.23823356 a
-0.59916772 0.98744470 b
0.29454513 0.74139013 a
0.38979430 0.08934727 b
-1.20807618 -0.95494386 a
-0.36367602 -0.19515038 b
-1.62667268 0.92552126 a
-0.25647839 0.48297852 b
在没有任何 group_by() 操作的情况下操纵 df 并使用 pander:
# make a table and output data
df_nogroup <- df %>%
mutate(xy = x * y) %>%
summarise(mean = mean(xy, na.rm = TRUE),
sd = sd(xy, na.rm = TRUE),
se = sd(xy, na.rm = TRUE)/sqrt(n()),
CI95_upr = mean + (qnorm(0.975) * se),
CI95_lwr = mean - (qnorm(0.975) * se),
n = n())
pander(df_nogroup, "No grouping step. Round working")
------------------------------------------
mean sd se CI95_upr CI95_lwr n
------ ---- ---- ---------- ---------- ---
-0.05 0.68 0.21 0.37 -0.47 10
------------------------------------------
Table: No grouping step. Round working
现在 group_by():
df_group <- df %>%
mutate(xy = x * y) %>%
group_by(class) %>%
summarise(mean = mean(xy, na.rm = TRUE),
sd = sd(xy, na.rm = TRUE),
se = sd(xy, na.rm = TRUE)/sqrt(n()),
CI95_upr = mean + (qnorm(0.975) * se),
CI95_lwr = mean - (qnorm(0.975) * se),
n = n())
pander(df_group, "Grouping appears to be the culprit")
--------------------------------------------------------
class mean sd se CI95_upr CI95_lwr n
------- -------- ------ ------ ---------- ---------- ---
a 0.04277 0.9674 0.4326 0.89069 -0.8051 5
b -0.14979 0.2640 0.1181 0.08163 -0.3812 5
--------------------------------------------------------
Table: Grouping appears to be the culprit
我的sessionInfo():
R version 3.3.2 (2016-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252 [4] LC_NUMERIC=C LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] pander_0.6.0 lsmeans_2.25 estimability_1.2 lme4_1.1-12 Matrix_1.2-7.1 lubridate_1.6.0 [7] dplyr_0.5.0 purrr_0.2.2 readr_1.0.0 tidyr_0.6.1 tibble_1.2 ggplot2_2.2.1 [13] tidyverse_1.1.0 knitr_1.15.1
解决方案
更新至dev pander 0.6.0后,问题已修复
我 运行 很喜欢这个,你必须设置内联选项而不是使用 panderOptions()
library(dplyr)
library(pander)
set.seed(10)
df <- data.frame(x = rnorm(10), y = rnorm(10), class = c("a", "b"))
df_group <- df %>%
mutate(xy = x * y) %>%
group_by(class) %>%
summarise(mean = mean(xy, na.rm = TRUE),
sd = sd(xy, na.rm = TRUE),
se = sd(xy, na.rm = TRUE)/sqrt(n()),
CI95_upr = mean + (qnorm(0.975) * se),
CI95_lwr = mean - (qnorm(0.975) * se),
n = n())
pander(df_group, "Setting inline options fixes this", round = 2)
-------------------------------------------------------
class mean sd se CI95_upr CI95_lwr n
------- ------- ------ ------ ---------- ---------- ---
a 0.04 0.97 0.43 0.89 -0.81 5
b -0.15 0.26 0.12 0.08 -0.38 5
-------------------------------------------------------
Table: Setting inline options fixes this
用于比较的会话信息。我正在使用 dplyr
.
的开发版本
> sessionInfo()
R version 3.3.2 (2016-10-31)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X El Capitan 10.11.6
locale:
[1] en_CA.UTF-8/en_CA.UTF-8/en_CA.UTF-8/C/en_CA.UTF-8/en_CA.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] bindrcpp_0.1 pander_0.6.0 dplyr_0.5.0.9000
loaded via a namespace (and not attached):
[1] lazyeval_0.2.0 magrittr_1.5 R6_2.2.0 assertthat_0.1 DBI_0.5-1 tools_3.3.2 tibble_1.2 Rcpp_0.12.9 digest_0.6.11 bindr_0.1
我不确定这是错误还是我做错了什么,但是当我在在将数据框输入 pander 之前。我在 Rstudio Notebook 中工作。示例如下:
加载库并设置选项:
library(pander)
panderOptions('round', 2)
panderOptions('keep.trailing.zeros', TRUE)
panderOptions('table.split.table', Inf)
library(tidyverse)
创建一些数据:
set.seed(10)
df <- data.frame(x = rnorm(10), y = rnorm(10), class = c("a", "b"))
print(df)
x y class
0.01874617 1.10177950 a
-0.18425254 0.75578151 b
-1.37133055 -0.23823356 a
-0.59916772 0.98744470 b
0.29454513 0.74139013 a
0.38979430 0.08934727 b
-1.20807618 -0.95494386 a
-0.36367602 -0.19515038 b
-1.62667268 0.92552126 a
-0.25647839 0.48297852 b
在没有任何 group_by() 操作的情况下操纵 df 并使用 pander:
# make a table and output data
df_nogroup <- df %>%
mutate(xy = x * y) %>%
summarise(mean = mean(xy, na.rm = TRUE),
sd = sd(xy, na.rm = TRUE),
se = sd(xy, na.rm = TRUE)/sqrt(n()),
CI95_upr = mean + (qnorm(0.975) * se),
CI95_lwr = mean - (qnorm(0.975) * se),
n = n())
pander(df_nogroup, "No grouping step. Round working")
------------------------------------------
mean sd se CI95_upr CI95_lwr n
------ ---- ---- ---------- ---------- ---
-0.05 0.68 0.21 0.37 -0.47 10
------------------------------------------
Table: No grouping step. Round working
现在 group_by():
df_group <- df %>%
mutate(xy = x * y) %>%
group_by(class) %>%
summarise(mean = mean(xy, na.rm = TRUE),
sd = sd(xy, na.rm = TRUE),
se = sd(xy, na.rm = TRUE)/sqrt(n()),
CI95_upr = mean + (qnorm(0.975) * se),
CI95_lwr = mean - (qnorm(0.975) * se),
n = n())
pander(df_group, "Grouping appears to be the culprit")
--------------------------------------------------------
class mean sd se CI95_upr CI95_lwr n
------- -------- ------ ------ ---------- ---------- ---
a 0.04277 0.9674 0.4326 0.89069 -0.8051 5
b -0.14979 0.2640 0.1181 0.08163 -0.3812 5
--------------------------------------------------------
Table: Grouping appears to be the culprit
我的sessionInfo():
R version 3.3.2 (2016-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252 [4] LC_NUMERIC=C LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] pander_0.6.0 lsmeans_2.25 estimability_1.2 lme4_1.1-12 Matrix_1.2-7.1 lubridate_1.6.0 [7] dplyr_0.5.0 purrr_0.2.2 readr_1.0.0 tidyr_0.6.1 tibble_1.2 ggplot2_2.2.1 [13] tidyverse_1.1.0 knitr_1.15.1
解决方案 更新至dev pander 0.6.0后,问题已修复
我 运行 很喜欢这个,你必须设置内联选项而不是使用 panderOptions()
library(dplyr)
library(pander)
set.seed(10)
df <- data.frame(x = rnorm(10), y = rnorm(10), class = c("a", "b"))
df_group <- df %>%
mutate(xy = x * y) %>%
group_by(class) %>%
summarise(mean = mean(xy, na.rm = TRUE),
sd = sd(xy, na.rm = TRUE),
se = sd(xy, na.rm = TRUE)/sqrt(n()),
CI95_upr = mean + (qnorm(0.975) * se),
CI95_lwr = mean - (qnorm(0.975) * se),
n = n())
pander(df_group, "Setting inline options fixes this", round = 2)
-------------------------------------------------------
class mean sd se CI95_upr CI95_lwr n
------- ------- ------ ------ ---------- ---------- ---
a 0.04 0.97 0.43 0.89 -0.81 5
b -0.15 0.26 0.12 0.08 -0.38 5
-------------------------------------------------------
Table: Setting inline options fixes this
用于比较的会话信息。我正在使用 dplyr
.
> sessionInfo()
R version 3.3.2 (2016-10-31)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X El Capitan 10.11.6
locale:
[1] en_CA.UTF-8/en_CA.UTF-8/en_CA.UTF-8/C/en_CA.UTF-8/en_CA.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] bindrcpp_0.1 pander_0.6.0 dplyr_0.5.0.9000
loaded via a namespace (and not attached):
[1] lazyeval_0.2.0 magrittr_1.5 R6_2.2.0 assertthat_0.1 DBI_0.5-1 tools_3.3.2 tibble_1.2 Rcpp_0.12.9 digest_0.6.11 bindr_0.1