You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用R的knitr和kableExtra将一维长度为2的三维数据转为美观kable表格

Solution for 3D Data (1 Dimension Length 2) with knitr & kableExtra

Great question—this is such a common use case for summary statistics, and kableExtra has all the tools you need to replicate that clean LaTeX-style table in R. Let’s break this down with a practical example, since seeing it in action makes it easier to adapt to your own data.

Step 1: Prepare Your 3D Data

First, let’s create a sample dataset that matches your scenario: 3 dimensions (Groups, Metrics, and a length-2 dimension like "Before/After" time points). We’ll use tidyverse to shape it:

library(tidyverse)
library(knitr)
library(kableExtra)

# Set seed for reproducibility
set.seed(123)

# Create 3D data frame
summary_data <- expand_grid(
  Group = c("Control", "Treatment 1", "Treatment 2"),
  Metric = c("Mean", "Standard Deviation", "Median"),
  Time_Point = c("Baseline", "Follow-Up") # Our length-2 dimension
) %>%
  mutate(Statistic = round(rnorm(nrow(.), mean = 50, sd = 8), 1))

Step 2: Reshape for Table Display

We need to convert the 3D data into a wide format that plays nicely with kableExtra. For this example, we’ll pivot the length-2 Time_Point into separate columns:

wide_data <- summary_data %>%
  pivot_wider(
    names_from = Time_Point,
    values_from = Statistic
  )

Step 3: Build the Formatted Table

Now we’ll use kable and kableExtra to add the clean, grouped look similar to LaTeX. We’ll use collapse_rows to merge repeated group names, making the table easier to read:

For HTML Output (e.g., R Markdown HTML docs):

wide_data %>%
  kable(
    format = "html",
    caption = "Summary Statistics by Group and Time Point",
    align = "c"
  ) %>%
  kable_styling(
    full_width = FALSE,
    bootstrap_options = c("striped", "hover")
  ) %>%
  collapse_rows(columns = 1, valign = "middle") # Merge group rows

For LaTeX Output (e.g., R Markdown PDFs):

wide_data %>%
  kable(
    format = "latex",
    caption = "Summary Statistics by Group and Time Point",
    align = "c",
    booktabs = TRUE
  ) %>%
  kable_styling(
    latex_options = c("striped", "hold_position")
  ) %>%
  collapse_rows(columns = 1, valign = "middle")

Alternative: Use Header Groups for the Length-2 Dimension

If you prefer to nest the length-2 dimension under column headers (instead of rows), you can reshape the data to group by your other dimension and use add_header_above:

# Reshape to group metrics under each group's time points
header_group_data <- summary_data %>%
  pivot_wider(
    names_from = c(Group, Time_Point),
    values_from = Statistic
  )

# Build table with nested headers
header_group_data %>%
  kable(
    format = "html",
    caption = "Summary Statistics with Time Point as Nested Headers",
    align = "c"
  ) %>%
  kable_styling(full_width = FALSE) %>%
  # Add top-level header for time points
  add_header_above(c(" ", "Control" = 2, "Treatment 1" = 2, "Treatment 2" = 2)) %>%
  add_header_above(c(" ", "Baseline/Follow-Up" = 6))

Both approaches will give you that polished, "3D-aware" table without needing to manually tweak LaTeX code directly in R.

内容的提问来源于stack exchange,提问作者Will Cornwell

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 03:46:20