英文:
Group rows by a variable in R (knitr::kable)
问题
我正在尝试按"Person"列中的值对kable
输出的行进行分组,以便更容易阅读表格输出。
MRE数据(在R Markdown文档中,使用R Studio 2022.07.1,Mac OS Ventura 13.2)
library("tidyverse")
library("knitr")
Person <- c("A", "A", "B", "B", "C", "C")
Group <- c("pre", "post", "pre", "post", "pre", "post")
Value <- c("10", "5", "8", "4", "5", "4")
df <- tibble(Person, Group, Value)
knitr::kable(df, format = "pipe")
在这个输出中,每行显示了其"Person"值。
我已经看到可以使用pack_rows()
或group_rows()
手动定义分组,但我希望这些分组按"Person"值分组,而不是不得不定义每个"Person"及其相关的两行。
英文:
I'm trying to group the rows of the kable
output by the value in the Person column, so the table output is easier to read.
Data for MRE (within an R markdown document, using R Studio 2022.07.1 on Mac OS Ventura 13.2)
library ("tidyverse")
library ("knitr")
Person <- c("A", "A", "B", "B", "C", "C")
Group <- c("pre", "post", "pre", "post", "pre", "post")
Value <- c("10", "5", "8", "4", "5", "4")
df <- tibble(Login,Group,Value)
knitr::kable(df, format = "pipe")
In this output, each row displays its Person value.
I've seen how you can use pack_rows()
or group_rows()
to manually define groups, but I would like this to be grouped by Person value, rather than be having to define each Person and their relevant two rows.
The current output looks like this
My desired output looks more like this
答案1
得分: 3
你还可以使用 pivot_wider
将数据转换为宽格式,代码如下:
Person <- c("A", "A", "B", "B", "C", "C")
Group <- c("pre", "post", "pre", "post", "pre", "post")
Value <- c("10", "5", "8", "4", "5", "4")
library(tibble)
library(tidyr)
df <- tibble(Person, Group, Value)
df <- pivot_wider(df, names_from = Group, values_from = Value)
knitr::kable(df, format = "pipe")
Person | pre | post |
---|---|---|
A | 10 | 5 |
B | 8 | 4 |
C | 5 | 4 |
创建于 2023-02-06,使用 reprex v2.0.2。
英文:
You could also transform your data to a wider format using pivot_wider
like this:
Person <- c("A", "A", "B", "B", "C", "C")
Group <- c("pre", "post", "pre", "post", "pre", "post")
Value <- c("10", "5", "8", "4", "5", "4")
library(tibble)
library(tidyr)
df <- tibble(Person,Group,Value)
df <- pivot_wider(df, names_from = Group, values_from = Value)
knitr::kable(df, format = "pipe")
Person | pre | post |
---|---|---|
A | 10 | 5 |
B | 8 | 4 |
C | 5 | 4 |
<sup>Created on 2023-02-06 with reprex v2.0.2</sup>
答案2
得分: 2
Using kableExtra::collapse_rows()...
This approach assumes you are printing to pdf.
There are multiple options within kableExtra to finetune the appearance of the table.
library(tibble)
library (kableExtra)
Person <- c("A", "A", "B", "B", "C", "C")
Group <- c("pre", "post", "pre", "post", "pre", "post")
Value <- c("10", "5", "8", "4", "5", "4")
df <- tibble(Person, Group, Value)
kbl(df, booktabs = TRUE) |>
collapse_rows(valign = "top",
latex_hline = "major")
英文:
Using kableExtra::collapse_rows()...
This approach assumes you are printing to pdf.
There are multiple options within kableExtra to finetune the appearance of the table.
---
output: pdf_document
---
```{r}
library(tibble)
library (kableExtra)
Person <- c("A", "A", "B", "B", "C", "C")
Group <- c("pre", "post", "pre", "post", "pre", "post")
Value <- c("10", "5", "8", "4", "5", "4")
df <- tibble(Person, Group, Value)
kbl(df, booktabs = TRUE) |>
collapse_rows(valign = "top",
latex_hline = "major")
```
答案3
得分: 1
以下是代码部分的翻译:
你也可以使用xtabs:
*首先将“Value”更改为数值类型
df$Value <- as.numeric(df$Value)
xtabs(Value ~ Group + Person)
如果你想以各种不同的格式呈现它,可以参考以下方式:
Library(gtsummary)
tbl_strata <- df %>%
tbl_strata(
strata = Person,
.tbl_fun =
~ .x %>%
tbl_summary(by = Group,
type = list(Value ~ "continuous"),
statistic = all_continuous() ~ "{sum}")
)
tbl_strata
*请注意,当使用小型数据集时,必须指定连续变量应如何呈现
这种格式也许更接近您所想要的吗?
**编辑**:
您还可以使用huxtable。它们也与Rmarkdown非常兼容。
好的,看看huxtables。它们还具有与Rmarkdown良好兼容的附加好处。
df <- df %>% hux() %>%
merge_repeated_rows()%>%
set_bottom_border(, row = odds, col = everywhere)
df
希望这对你有所帮助。
英文:
You could also use xtabs:
*change "Value" to numeric first
df$Value <- as.numeric(df$Value)
xtabs(Value ~ Group + Person)
and if you want to present it in all various formats, something along the lines of :
Library(gtsummary)
tbl_strata <- df %>%tbl_strata(
strata = Person,
.tbl_fun =
~ .x %>%
tbl_summary(by = Group,
type = list(Value ~ "continuous"),
statistic = all_continuous() ~ "{sum}")
)
tbl_strata
- note that you have to specify that continuous variables should be presented as such when using small datasets
is this format maybe a bit closer to what you had in mind?
edit:
you can also use a huxtable. They are also nicely compatible with Rmakrdown
okay, have a look at huxtables. they have the added benefit of also beign nicely compatible with Rmarkdown.
df <- df %>% hux() %>%
merge_repeated_rows()%>%
set_bottom_border(, row = odds, col = everywhere)
df
答案4
得分: 0
这是代码部分,不需要翻译:
library("kableExtra")
Person <- c("A", "A", "B", "B", "C", "C")
Group <- c("pre", "post", "pre", "post", "pre", "post")
Value <- c("10", "5", "8", "4", "5", "4")
df <- tibble(Person, Group, Value)
df[,c("Group","Value")] %>%
kbl(table.attr = "style='width:25%;'", caption = "Caption", align = "c") %>%
kable_styling(bootstrap_options = "striped", font_size = 12, position = "center") %>%
pack_rows(index=rowSums(table(df$Person,df$Group)))
如果您有任何其他问题,欢迎提出。
英文:
I am late on this but I was dealing with a similar issue that I found a solution for. This is for anyone else who might need this as well.
library("kableExtra")
Person <- c("A", "A", "B", "B", "C", "C")
Group <- c("pre", "post", "pre", "post", "pre", "post")
Value <- c("10", "5", "8", "4", "5", "4")
df <- tibble(Person,Group,Value)
df[,c("Group","Value")] %>%
kbl(table.attr = "style='width:25%;'", caption = "Caption",align = "c") %>%
kable_styling(bootstrap_options = "striped", font_size = 12, position = "center") %>%
pack_rows(index=rowSums(table(df$Person,df$Group)))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论