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

如何在Kable中设置重复行名?R Markdown表格行名配置问题

问题:在R Markdown的kable表格中实现重复行名显示

在使用R Markdown的kable生成表格时,需要将数据框的第2、5、8、11、14行统一显示为“Tattooed”,第3、6、9、12、15行统一显示为“Prominently Tattooed”。但直接设置重复行名会触发报错,目前只能通过添加后缀(如(T)、(R))的临时方案规避,希望去掉后缀实现重复行名的显示需求。

原代码(触发报错)

table_anova_posthoc <- data.frame(
  matrix(
    nrow = 18, ncol = 8)
  )

rownames(table_anova_posthoc) <- c("Trust", "Tattooed", "Prominently Tattooed", "Responsibility", "Tattooed", "Prominently Tattooed", "Negative Emotionality", "Tattooed", "Prominently Tattooed", "Depression", "Tattooed", "Prominently Tattooed", "Emotional Volatility", "Tattooed", "Prominently Tattooed", "Self-Esteem", "Tattooed", "Prominently Tattooed")

报错信息

Quitting from lines 1593-1799 [unnamed-chunk-5] (Master-Thesis.Rmd)
Error in `.rowNamesDF<-`:
! duplicate 'row.names' are not allowed
Backtrace:
 1. base::`rownames<-`(`*tmp*`, value = `<chr>`)
 3. base::`row.names<-.data.frame`(`*tmp*`, value = value)
 4. base::`.rowNamesDF<-`(x, value = value)
Execution halted

临时方案(添加后缀规避报错)

rownames(table_anova_posthoc) <- c("Trust", "Tattooed (T)", "Prominently Tattooed (T)", "Responsibility", "Tattooed (R)", "Prominently Tattooed (R)", "Negative Emotionality", "Tattooed (NE)", "Prominently Tattooed (NE)", "Depression", "Tattooed (D)", "Prominently Tattooed (D)", "Emotional Volatility", "Tattooed (EV)", "Prominently Tattooed (EV)", "Self-Esteem", "Tattooed (SE)", "Prominently Tattooed (SE)")

解决方法

数据框本身要求行名唯一,没必要强行设置重复行名,直接在生成kable表格时自定义显示文本即可,以下两种方法都可行:

方法1:在kable中直接替换显示的行名

先保留数据框的唯一行名(比如用临时方案的带后缀名),调用kable()时通过row.names参数传入想要显示的重复行名向量:

library(kableExtra)

# 保留临时方案的唯一行名
rownames(table_anova_posthoc) <- c("Trust", "Tattooed (T)", "Prominently Tattooed (T)", "Responsibility", "Tattooed (R)", "Prominently Tattooed (R)", "Negative Emotionality", "Tattooed (NE)", "Prominently Tattooed (NE)", "Depression", "Tattooed (D)", "Prominently Tattooed (D)", "Emotional Volatility", "Tattooed (EV)", "Prominently Tattooed (EV)", "Self-Esteem", "Tattooed (SE)", "Prominently Tattooed (SE)")

# 定义要显示的行名向量
display_names <- c("Trust", "Tattooed", "Prominently Tattooed", "Responsibility", "Tattooed", "Prominently Tattooed", "Negative Emotionality", "Tattooed", "Prominently Tattooed", "Depression", "Tattooed", "Prominently Tattooed", "Emotional Volatility", "Tattooed", "Prominently Tattooed", "Self-Esteem", "Tattooed", "Prominently Tattooed")

# 生成表格时指定显示的行名
kable(table_anova_posthoc, row.names = display_names) %>%
  kable_styling()

方法2:将行标签转为数据框的一列(更推荐)

把需要显示的行标签作为数据框的第一列,彻底绕开行名必须唯一的限制,生成表格时隐藏默认行名即可:

library(kableExtra)

# 重新构造数据框,添加行标签列
table_anova_posthoc <- data.frame(
  行标签 = c("Trust", "Tattooed", "Prominently Tattooed", "Responsibility", "Tattooed", "Prominently Tattooed", "Negative Emotionality", "Tattooed", "Prominently Tattooed", "Depression", "Tattooed", "Prominently Tattooed", "Emotional Volatility", "Tattooed", "Prominently Tattooed", "Self-Esteem", "Tattooed", "Prominently Tattooed"),
  matrix(nrow = 18, ncol = 8)
)

# 生成表格,隐藏默认行名,用自定义列作为行标签
kable(table_anova_posthoc, row.names = FALSE) %>%
  column_spec(1, bold = TRUE) %>% # 可选:给行标签加粗增强可读性
  kable_styling()

这种方法更合理,因为行名本质是数据框的索引,用来存储显示文本本来就不合适,把标签放到数据列里自由度更高。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 01:12:16