我如何让 gt table 与 get_summary_stats 一起工作
How do I get gt table to work with get_summary_stats
数据集
这是我的数据集的输入头:
structure(list(Year = c("2021", "2021", "2021", "2021", "2021",
"2021"), Month_Number = c("9", "9", "9", "9", "9", "9"), Month_Name = c("September",
"September", "September", "September", "September", "September"
), Day_Name = c("Wednesday", "Thursday", "Friday", "Saturday",
"Sunday", "Monday"), Time_Wake = c(500L, 715L, 600L, 600L, 700L,
600L), Mins_Sleep = c(300L, 540L, 540L, 480L, 480L, 480L), Start_Work = c(1015L,
1000L, 945L, 1400L, 1500L, 915L), End_Work = c(1800L, 1600L,
1210L, 1700L, 1515L, 1530L), Workout_Y_N = c("Y", "Y", "Y", "N",
"N", "N"), Time_Workout = c(730L, 730L, 730L, NA, NA, NA), Work_Environment = c("Office",
"Office", "Office", "Home", "Home", "Office"), Coffee_Cups = c(3L,
0L, 2L, 6L, 4L, 5L), Tea_Cups = c(2L, 4L, 2L, 0L, 0L, 2L), Mins_Work = c(435L,
350L, 145L, 135L, 15L, 60L)), row.names = c(NA, 6L), class = "data.frame")
脚本
我正在尝试 运行 table 我的摘要统计数据,但在使用 gt 库生成它时遇到问题。这是我的脚本:
library(tidyverse)
library(gt)
library(rstatix)
work %>%
get_summary_stats() %>%
gt() %>%
tab_header(
title = md("The ***productivity*** dataset"),
subtitle = md("Descriptives of five months of data on productivity, consumption, and sleep patterns.")
) %>%
cols_label(
n = md("Cases (N)"),
min = md("Min"),
max = md("Max"),
median = md("Median"),
q1 = md("Q1"),
q3 = md("Q3"),
iqr = md("IQR"),
mad = md("MAD"),
mean = md("Mean"),
sd = md("SD"),
se = md("SE"),
ci = md("CI")
) %>%
opt_align_table_header(align = "center") %>%
tab_source_note(
source_note = md("*Data obtained from local matrix.*")
) %>%
tab_footnote(
footnote = "Not a standardized unit.",
locations = cells_stub(rows ="Coffee_Cups")
)
问题
问题是虽然此脚本生成 table,但我无法使脚注起作用,因为它无法将“Coffee_Cups”识别为一行。这是没有 tab_footnote
命令的样子:
这是它给我的错误,没有多大帮助:
Error: The following row(s) do not exist in the data: Coffee_Cups
我尝试修改 tab_footnote
中的其他子命令,但没有成功 运行。我该怎么办?
我在这里评论,因为我需要更多字符。
如果您运行满足以下条件:
> w$`_data`
# A tibble: 8 × 13
variable n min max median q1 q3 iqr mad mean sd se ci
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Coffee_Cups 6 0 6 3.5 2.25 4.75 2.5 2.22 3.33 2.16 0.882 2.27
2 End_Work 6 1210 1800 1565 1519. 1675 156. 137. 1559. 202. 82.5 212.
3 Mins_Sleep 6 300 540 480 480 525 45 44.5 470 88.3 36.1 92.7
4 Mins_Work 6 15 435 140 78.8 299. 220 152. 190 166. 67.9 174.
5 Start_Work 6 915 1500 1008. 959. 1304. 345 115. 1129. 253. 103. 266.
6 Tea_Cups 6 0 4 2 0.5 2 1.5 1.48 1.67 1.51 0.615 1.58
7 Time_Wake 6 500 715 600 600 675 75 74.1 619. 78.8 32.2 82.7
8 Time_Workout 3 730 730 730 730 730 0 0 730 0 0 0
在那里你可以清楚地看到 Coffee_Cups 是列中的值,而不是行中的值。
我试过像这样更改代码:
tab_footnote(
footnote = "Not a standardized unit.",
locations = cells_stub(rows = c(1))
)
我收到以下错误:
Error: Can't use NA as column index with `[` at position 1.
Run `rlang::last_error()` to see where the error occurred.
> rlang::last_error()
<error/tibble_error_na_column_index>
Can't use NA as column index with `[` at position 1.
Backtrace:
1. (function (x, ...) ...
2. gt:::print.gt_tbl(x)
3. gt:::as.tags.gt_tbl(x, ...)
4. gt:::render_as_html(data = x)
5. gt:::build_data(data = data, context = "html")
6. gt:::apply_footnotes_to_output(data = data, context = context)
8. tibble:::`[.tbl_df`(body, footnotes_data_marks$rownum[i], footnotes_data_marks$colname[i])
Run `rlang::last_trace()` to see the full context.
此时我们知道该行已被占用,现在我们需要找出列不匹配的问题...
我可以使用以下方法让它工作:
works %>%
get_summary_stats() %>%
gt() %>%
tab_header(
title = md("The ***productivity*** dataset"),
subtitle = md("Descriptives of five months of data on productivity, consumption, and sleep patterns.")
) %>%
cols_label(
n = md("Cases (N)"),
min = md("Min"),
max = md("Max"),
median = md("Median"),
q1 = md("Q1"),
q3 = md("Q3"),
iqr = md("IQR"),
mad = md("MAD"),
mean = md("Mean"),
sd = md("SD"),
se = md("SE"),
ci = md("CI")
) %>%
opt_align_table_header(align = "center") %>%
tab_source_note(
source_note = md("*Data obtained from local matrix.*")
) %>%
tab_footnote(
footnote = "Not a standardized unit.",
#locations = cells_stub(rows = everything(vars = "Coffee_Cups"))
locations = cells_body(1,1)
)
数据集
这是我的数据集的输入头:
structure(list(Year = c("2021", "2021", "2021", "2021", "2021",
"2021"), Month_Number = c("9", "9", "9", "9", "9", "9"), Month_Name = c("September",
"September", "September", "September", "September", "September"
), Day_Name = c("Wednesday", "Thursday", "Friday", "Saturday",
"Sunday", "Monday"), Time_Wake = c(500L, 715L, 600L, 600L, 700L,
600L), Mins_Sleep = c(300L, 540L, 540L, 480L, 480L, 480L), Start_Work = c(1015L,
1000L, 945L, 1400L, 1500L, 915L), End_Work = c(1800L, 1600L,
1210L, 1700L, 1515L, 1530L), Workout_Y_N = c("Y", "Y", "Y", "N",
"N", "N"), Time_Workout = c(730L, 730L, 730L, NA, NA, NA), Work_Environment = c("Office",
"Office", "Office", "Home", "Home", "Office"), Coffee_Cups = c(3L,
0L, 2L, 6L, 4L, 5L), Tea_Cups = c(2L, 4L, 2L, 0L, 0L, 2L), Mins_Work = c(435L,
350L, 145L, 135L, 15L, 60L)), row.names = c(NA, 6L), class = "data.frame")
脚本
我正在尝试 运行 table 我的摘要统计数据,但在使用 gt 库生成它时遇到问题。这是我的脚本:
library(tidyverse)
library(gt)
library(rstatix)
work %>%
get_summary_stats() %>%
gt() %>%
tab_header(
title = md("The ***productivity*** dataset"),
subtitle = md("Descriptives of five months of data on productivity, consumption, and sleep patterns.")
) %>%
cols_label(
n = md("Cases (N)"),
min = md("Min"),
max = md("Max"),
median = md("Median"),
q1 = md("Q1"),
q3 = md("Q3"),
iqr = md("IQR"),
mad = md("MAD"),
mean = md("Mean"),
sd = md("SD"),
se = md("SE"),
ci = md("CI")
) %>%
opt_align_table_header(align = "center") %>%
tab_source_note(
source_note = md("*Data obtained from local matrix.*")
) %>%
tab_footnote(
footnote = "Not a standardized unit.",
locations = cells_stub(rows ="Coffee_Cups")
)
问题
问题是虽然此脚本生成 table,但我无法使脚注起作用,因为它无法将“Coffee_Cups”识别为一行。这是没有 tab_footnote
命令的样子:
这是它给我的错误,没有多大帮助:
Error: The following row(s) do not exist in the data: Coffee_Cups
我尝试修改 tab_footnote
中的其他子命令,但没有成功 运行。我该怎么办?
我在这里评论,因为我需要更多字符。
如果您运行满足以下条件:
> w$`_data`
# A tibble: 8 × 13
variable n min max median q1 q3 iqr mad mean sd se ci
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Coffee_Cups 6 0 6 3.5 2.25 4.75 2.5 2.22 3.33 2.16 0.882 2.27
2 End_Work 6 1210 1800 1565 1519. 1675 156. 137. 1559. 202. 82.5 212.
3 Mins_Sleep 6 300 540 480 480 525 45 44.5 470 88.3 36.1 92.7
4 Mins_Work 6 15 435 140 78.8 299. 220 152. 190 166. 67.9 174.
5 Start_Work 6 915 1500 1008. 959. 1304. 345 115. 1129. 253. 103. 266.
6 Tea_Cups 6 0 4 2 0.5 2 1.5 1.48 1.67 1.51 0.615 1.58
7 Time_Wake 6 500 715 600 600 675 75 74.1 619. 78.8 32.2 82.7
8 Time_Workout 3 730 730 730 730 730 0 0 730 0 0 0
在那里你可以清楚地看到 Coffee_Cups 是列中的值,而不是行中的值。
我试过像这样更改代码:
tab_footnote(
footnote = "Not a standardized unit.",
locations = cells_stub(rows = c(1))
)
我收到以下错误:
Error: Can't use NA as column index with `[` at position 1.
Run `rlang::last_error()` to see where the error occurred.
> rlang::last_error()
<error/tibble_error_na_column_index>
Can't use NA as column index with `[` at position 1.
Backtrace:
1. (function (x, ...) ...
2. gt:::print.gt_tbl(x)
3. gt:::as.tags.gt_tbl(x, ...)
4. gt:::render_as_html(data = x)
5. gt:::build_data(data = data, context = "html")
6. gt:::apply_footnotes_to_output(data = data, context = context)
8. tibble:::`[.tbl_df`(body, footnotes_data_marks$rownum[i], footnotes_data_marks$colname[i])
Run `rlang::last_trace()` to see the full context.
此时我们知道该行已被占用,现在我们需要找出列不匹配的问题...
我可以使用以下方法让它工作:
works %>%
get_summary_stats() %>%
gt() %>%
tab_header(
title = md("The ***productivity*** dataset"),
subtitle = md("Descriptives of five months of data on productivity, consumption, and sleep patterns.")
) %>%
cols_label(
n = md("Cases (N)"),
min = md("Min"),
max = md("Max"),
median = md("Median"),
q1 = md("Q1"),
q3 = md("Q3"),
iqr = md("IQR"),
mad = md("MAD"),
mean = md("Mean"),
sd = md("SD"),
se = md("SE"),
ci = md("CI")
) %>%
opt_align_table_header(align = "center") %>%
tab_source_note(
source_note = md("*Data obtained from local matrix.*")
) %>%
tab_footnote(
footnote = "Not a standardized unit.",
#locations = cells_stub(rows = everything(vars = "Coffee_Cups"))
locations = cells_body(1,1)
)