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

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

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"及其相关的两行。

当前的输出如下所示在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)

library (&quot;tidyverse&quot;)
library (&quot;knitr&quot;)

Person &lt;- c(&quot;A&quot;, &quot;A&quot;, &quot;B&quot;, &quot;B&quot;, &quot;C&quot;, &quot;C&quot;)
Group &lt;- c(&quot;pre&quot;, &quot;post&quot;, &quot;pre&quot;, &quot;post&quot;, &quot;pre&quot;, &quot;post&quot;)
Value &lt;- c(&quot;10&quot;, &quot;5&quot;, &quot;8&quot;, &quot;4&quot;, &quot;5&quot;, &quot;4&quot;)

df &lt;- tibble(Login,Group,Value)

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 将数据转换为宽格式,代码如下:

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 &lt;- c(&quot;A&quot;, &quot;A&quot;, &quot;B&quot;, &quot;B&quot;, &quot;C&quot;, &quot;C&quot;)
Group &lt;- c(&quot;pre&quot;, &quot;post&quot;, &quot;pre&quot;, &quot;post&quot;, &quot;pre&quot;, &quot;post&quot;)
Value &lt;- c(&quot;10&quot;, &quot;5&quot;, &quot;8&quot;, &quot;4&quot;, &quot;5&quot;, &quot;4&quot;)

library(tibble)
library(tidyr)
df &lt;- tibble(Person,Group,Value)
df &lt;- pivot_wider(df, names_from = Group, values_from = Value)
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.


library(tibble)
library (kableExtra)

Person &lt;- c("A", "A", "B", "B", "C", "C")
Group &lt;- c("pre", "post", "pre", "post", "pre", "post")
Value &lt;- c("10", "5", "8", "4", "5", "4")

df &lt;- tibble(Person, Group, Value)

  kbl(df, booktabs = TRUE) |&gt; 
  collapse_rows(valign = "top",
                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.

---
output: pdf_document
---

```{r}

library(tibble)
library (kableExtra)

Person &lt;- c(&quot;A&quot;, &quot;A&quot;, &quot;B&quot;, &quot;B&quot;, &quot;C&quot;, &quot;C&quot;)
Group &lt;- c(&quot;pre&quot;, &quot;post&quot;, &quot;pre&quot;, &quot;post&quot;, &quot;pre&quot;, &quot;post&quot;)
Value &lt;- c(&quot;10&quot;, &quot;5&quot;, &quot;8&quot;, &quot;4&quot;, &quot;5&quot;, &quot;4&quot;)

df &lt;- tibble(Person, Group, Value)

  kbl(df, booktabs = TRUE) |&gt; 
  collapse_rows(valign = &quot;top&quot;,
                latex_hline = &quot;major&quot;)

```

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

答案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 &lt;- 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 &lt;- df %&gt;%tbl_strata(
strata = Person,
.tbl_fun =
  ~ .x %&gt;%
  tbl_summary(by = Group,
              type = list(Value ~ &quot;continuous&quot;),
              statistic = all_continuous() ~ &quot;{sum}&quot;)
)
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 &lt;- df %&gt;% hux() %&gt;%
   merge_repeated_rows()%&gt;%
   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(&quot;kableExtra&quot;)

Person &lt;- c(&quot;A&quot;, &quot;A&quot;, &quot;B&quot;, &quot;B&quot;, &quot;C&quot;, &quot;C&quot;)
Group &lt;- c(&quot;pre&quot;, &quot;post&quot;, &quot;pre&quot;, &quot;post&quot;, &quot;pre&quot;, &quot;post&quot;)
Value &lt;- c(&quot;10&quot;, &quot;5&quot;, &quot;8&quot;, &quot;4&quot;, &quot;5&quot;, &quot;4&quot;)

df &lt;- tibble(Person,Group,Value)

df[,c(&quot;Group&quot;,&quot;Value&quot;)] %&gt;% 
  kbl(table.attr = &quot;style=&#39;width:25%;&#39;&quot;, caption = &quot;Caption&quot;,align = &quot;c&quot;) %&gt;%  
  kable_styling(bootstrap_options = &quot;striped&quot;, font_size = 12, position = &quot;center&quot;) %&gt;% 
  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:

确定