英文:
Create a stacked 2 x 2 kable table in R with different dimension dataframes
问题
以下是您提供的代码的翻译部分:
我正在尝试在R中创建一组可以以HTML、LaTeX或PDF形式存在的表格,因此我正在使用kable。我想创建一个2行2列的表格,每个单元格包含来自不同数据框的信息。我希望在每个单元格的上方有不同的信息作为标题。对我来说棘手的部分是这些表格具有不同的宽度,所以我希望单元格的宽度或高度与我指定的一样,并且这将增加整个表格的宽度和高度。
基本上,我希望它看起来像这样:
![enter image description here](https://i.stack.imgur.com/IQEi7.png)
到目前为止,我有这段代码:
```R
library(kableExtra)
df1 <- head(mtcars)
df2 <- head(iris)
df3 <- head(ChickWeight)
df4 <- head(airquality)
table1 <- kable(df1, format = "html") %>%
kable_styling()
table2 <- kable(df2, format = "html") %>%
kable_styling()
table3 <- kable(df3, format = "html") %>%
kable_styling()
table4 <- kable(df4, format = "html") %>%
kable_styling()
combined_table=kable(list(df1,df2, df3,df4), format="html") %>%
kable_styling() %>%
column_spec(column=1:4, border_left = T, border_right = T)
但当它们具有不同的列长度时,我无法rbind数据帧。那么如何将它们放入不同的面板?
我包括了我的代码,希望它能将对象分成不同的面板,但它没有这样做。
请注意,这是您提供的代码的翻译部分,不包括代码的解决方案或答案。如果您需要关于如何解决问题的帮助,请随时提出具体的问题。
<details>
<summary>英文:</summary>
I am trying to create in R a set of tables that can be in either html or latex or pdf form so I'm using kable. I want to create a 2 row and 2 column table with each cell containing information from a different dataframe. I want headers above each of the cells with different information. The tricky part for me is the tables have different so I want the cells to be as wide or long as a I specify and this would just add to the width and height of the overall table.
Basically i want it to look like this
![enter image description here](https://i.stack.imgur.com/IQEi7.png)
I have this code so far
library(kableExtra)
df1 <- head(mtcars)
df2 <- head(iris)
df3 <- head(ChickWeight)
df4 <- head(airquality)
table1 <- kable(df1, format = "html") %>%
kable_styling()
table2 <- kable(df2, format = "html") %>%
kable_styling()
table3 <- kable(df3, format = "html") %>%
kable_styling()
table4 <- kable(df4, format = "html") %>%
kable_styling()
combined_table=kable(list(df1,df2, df3,df4), format="html") %>%
kable_styling() %>%
column_spec(column=1:4, border_left = T, border_right = T)
but I cannot rbind the dataframes when they have different column lengths. So how do I get them into different panels.
I included my code and expected it to panel the objects but it does not.
</details>
# 答案1
**得分**: 0
以下是使用 `quarto` 中的网格布局的方法:
format: html
execute:
echo: false
```{r}
library(kableExtra)
df1 <- head(mtcars)
df2 <- head(iris)
df3 <- head(ChickWeight)
df4 <- head(airquality)
table1 <- kable(df1, format = "html") %>%
kable_styling()
table2 <- kable(df2, format = "html") %>%
kable_styling()
table3 <- kable(df3, format = "html") %>%
kable_styling()
table4 <- kable(df4, format = "html") %>%
kable_styling()
::: {.grid}
::: {.g-col-6}
## 组合表格
```{r}
table1
:::
::: {.g-col-6}
{}
table2
:::
:::
::: {.grid}
::: {.g-col-6}
table3
:::
::: {.g-col-6}
table4
:::
:::
[![在这里输入图片描述][1]][1]
[1]: https://i.stack.imgur.com/yu3u1.png
<details>
<summary>英文:</summary>
Here is a way using the grid layout in `quarto`:
---
format: html
execute:
echo: false
---
```{r}
library(kableExtra)
df1 <- head(mtcars)
df2 <- head(iris)
df3 <- head(ChickWeight)
df4 <- head(airquality)
table1 <- kable(df1, format = "html") %>%
kable_styling()
table2 <- kable(df2, format = "html") %>%
kable_styling()
table3 <- kable(df3, format = "html") %>%
kable_styling()
table4 <- kable(df4, format = "html") %>%
kable_styling()
```
::: {.grid}
::: {.g-col-6}
## Combined Table
```{r}
table1
```
:::
::: {.g-col-6}
## {}
```{r}
table2
```
:::
:::
::: {.grid}
::: {.g-col-6}
```{r}
table3
```
:::
::: {.g-col-6}
```{r}
table4
```
:::
:::
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/yu3u1.png
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论