在R中按变量对行进行分组(knitr::kable)

huangapple go评论86阅读模式
英文:

Group rows by a variable in R (knitr::kable)

问题

我正在尝试按"Person"列中的值对kable输出的行进行分组,以便更容易阅读表格输出。

MRE数据(在R Markdown文档中,使用R Studio 2022.07.1,Mac OS Ventura 13.2)

  1. library("tidyverse")
  2. library("knitr")
  3. Person <- c("A", "A", "B", "B", "C", "C")
  4. Group <- c("pre", "post", "pre", "post", "pre", "post")
  5. Value <- c("10", "5", "8", "4", "5", "4")
  6. df <- tibble(Person, Group, Value)
  7. knitr::kable(df, format = "pipe")

在这个输出中,每行显示了其"Person"值。

我已经看到可以使用pack_rows()group_rows()手动定义分组,但我希望这些分组按"Person"值分组,而不是不得不定义每个"Person"及其相关的两行。

当前的输出如下所示在R中按变量对行进行分组(knitr::kable)

我期望的输出更像这样在R中按变量对行进行分组(knitr::kable)

英文:

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)

  1. library (&quot;tidyverse&quot;)
  2. library (&quot;knitr&quot;)
  3. Person &lt;- c(&quot;A&quot;, &quot;A&quot;, &quot;B&quot;, &quot;B&quot;, &quot;C&quot;, &quot;C&quot;)
  4. Group &lt;- c(&quot;pre&quot;, &quot;post&quot;, &quot;pre&quot;, &quot;post&quot;, &quot;pre&quot;, &quot;post&quot;)
  5. Value &lt;- c(&quot;10&quot;, &quot;5&quot;, &quot;8&quot;, &quot;4&quot;, &quot;5&quot;, &quot;4&quot;)
  6. df &lt;- tibble(Login,Group,Value)
  7. knitr::kable(df, format = &quot;pipe&quot;)

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在R中按变量对行进行分组(knitr::kable)

My desired output looks more like this

在R中按变量对行进行分组(knitr::kable)

答案1

得分: 3

你还可以使用 pivot_wider 将数据转换为宽格式,代码如下:

  1. Person <- c("A", "A", "B", "B", "C", "C")
  2. Group <- c("pre", "post", "pre", "post", "pre", "post")
  3. Value <- c("10", "5", "8", "4", "5", "4")
  4. library(tibble)
  5. library(tidyr)
  6. df <- tibble(Person, Group, Value)
  7. df <- pivot_wider(df, names_from = Group, values_from = Value)
  8. 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:

  1. Person &lt;- c(&quot;A&quot;, &quot;A&quot;, &quot;B&quot;, &quot;B&quot;, &quot;C&quot;, &quot;C&quot;)
  2. Group &lt;- c(&quot;pre&quot;, &quot;post&quot;, &quot;pre&quot;, &quot;post&quot;, &quot;pre&quot;, &quot;post&quot;)
  3. Value &lt;- c(&quot;10&quot;, &quot;5&quot;, &quot;8&quot;, &quot;4&quot;, &quot;5&quot;, &quot;4&quot;)
  4. library(tibble)
  5. library(tidyr)
  6. df &lt;- tibble(Person,Group,Value)
  7. df &lt;- pivot_wider(df, names_from = Group, values_from = Value)
  8. knitr::kable(df, format = &quot;pipe&quot;)
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.

  1. library(tibble)
  2. library (kableExtra)
  3. Person &lt;- c("A", "A", "B", "B", "C", "C")
  4. Group &lt;- c("pre", "post", "pre", "post", "pre", "post")
  5. Value &lt;- c("10", "5", "8", "4", "5", "4")
  6. df &lt;- tibble(Person, Group, Value)
  7. kbl(df, booktabs = TRUE) |&gt;
  8. collapse_rows(valign = "top",
  9. latex_hline = "major")

在R中按变量对行进行分组(knitr::kable)

英文:

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.

  1. ---
  2. output: pdf_document
  3. ---
  4. ```{r}
  5. library(tibble)
  6. library (kableExtra)
  7. Person &lt;- c(&quot;A&quot;, &quot;A&quot;, &quot;B&quot;, &quot;B&quot;, &quot;C&quot;, &quot;C&quot;)
  8. Group &lt;- c(&quot;pre&quot;, &quot;post&quot;, &quot;pre&quot;, &quot;post&quot;, &quot;pre&quot;, &quot;post&quot;)
  9. Value &lt;- c(&quot;10&quot;, &quot;5&quot;, &quot;8&quot;, &quot;4&quot;, &quot;5&quot;, &quot;4&quot;)
  10. df &lt;- tibble(Person, Group, Value)
  11. kbl(df, booktabs = TRUE) |&gt;
  12. collapse_rows(valign = &quot;top&quot;,
  13. latex_hline = &quot;major&quot;)
  14. ```

在R中按变量对行进行分组(knitr::kable)

答案3

得分: 1

以下是代码部分的翻译:

  1. 你也可以使用xtabs
  2. *首先将“Value”更改为数值类型
  3. df$Value <- as.numeric(df$Value)
  4. xtabs(Value ~ Group + Person)
  5. 如果你想以各种不同的格式呈现它,可以参考以下方式:
  6. Library(gtsummary)
  7. tbl_strata <- df %>%
  8. tbl_strata(
  9. strata = Person,
  10. .tbl_fun =
  11. ~ .x %>%
  12. tbl_summary(by = Group,
  13. type = list(Value ~ "continuous"),
  14. statistic = all_continuous() ~ "{sum}")
  15. )
  16. tbl_strata
  17. *请注意,当使用小型数据集时,必须指定连续变量应如何呈现
  18. 这种格式也许更接近您所想要的吗?
  19. **编辑**
  20. 您还可以使用huxtable。它们也与Rmarkdown非常兼容。
  21. 好的,看看huxtables。它们还具有与Rmarkdown良好兼容的附加好处。
  22. df <- df %>% hux() %>%
  23. merge_repeated_rows()%>%
  24. set_bottom_border(, row = odds, col = everywhere)
  25. df

希望这对你有所帮助。

英文:

You could also use xtabs:

*change "Value" to numeric first

  1. df$Value &lt;- as.numeric(df$Value)
  2. xtabs(Value ~ Group + Person)

and if you want to present it in all various formats, something along the lines of :

  1. Library(gtsummary)
  2. tbl_strata &lt;- df %&gt;%tbl_strata(
  3. strata = Person,
  4. .tbl_fun =
  5. ~ .x %&gt;%
  6. tbl_summary(by = Group,
  7. type = list(Value ~ &quot;continuous&quot;),
  8. statistic = all_continuous() ~ &quot;{sum}&quot;)
  9. )
  10. 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.

  1. df &lt;- df %&gt;% hux() %&gt;%
  2. merge_repeated_rows()%&gt;%
  3. set_bottom_border(, row = odds, col = everywhere)
  4. df

答案4

得分: 0

这是代码部分,不需要翻译:

  1. library("kableExtra")
  2. Person <- c("A", "A", "B", "B", "C", "C")
  3. Group <- c("pre", "post", "pre", "post", "pre", "post")
  4. Value <- c("10", "5", "8", "4", "5", "4")
  5. df <- tibble(Person, Group, Value)
  6. df[,c("Group","Value")] %>%
  7. kbl(table.attr = "style='width:25%;'", caption = "Caption", align = "c") %>%
  8. kable_styling(bootstrap_options = "striped", font_size = 12, position = "center") %>%
  9. 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.

  1. library(&quot;kableExtra&quot;)
  2. Person &lt;- c(&quot;A&quot;, &quot;A&quot;, &quot;B&quot;, &quot;B&quot;, &quot;C&quot;, &quot;C&quot;)
  3. Group &lt;- c(&quot;pre&quot;, &quot;post&quot;, &quot;pre&quot;, &quot;post&quot;, &quot;pre&quot;, &quot;post&quot;)
  4. Value &lt;- c(&quot;10&quot;, &quot;5&quot;, &quot;8&quot;, &quot;4&quot;, &quot;5&quot;, &quot;4&quot;)
  5. df &lt;- tibble(Person,Group,Value)
  6. df[,c(&quot;Group&quot;,&quot;Value&quot;)] %&gt;%
  7. kbl(table.attr = &quot;style=&#39;width:25%;&#39;&quot;, caption = &quot;Caption&quot;,align = &quot;c&quot;) %&gt;%
  8. kable_styling(bootstrap_options = &quot;striped&quot;, font_size = 12, position = &quot;center&quot;) %&gt;%
  9. pack_rows(index=rowSums(table(df$Person,df$Group)))

在R中按变量对行进行分组(knitr::kable)

huangapple
  • 本文由 发表于 2023年2月6日 19:24:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75360671.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定