ksinva Asked: 2024-04-05 00:26:13 +0800 CST2024-04-05 00:26:13 +0800 CST 2024-04-05 00:26:13 +0800 CST 在“gt”表中添加第二个“groupname_col”,而不连接列值 772 我喜欢gt将行组分成列的功能,但我希望将两个行组作为列,而不是一列连接两个组(这是默认设置)。例如: library(gt) gtcars %>% group_by(mfr, year) %>% gt(row_group_as_column = TRUE) 这为我们提供了一个gt如下所示的表格,其中品牌和年份连接到一列中。有没有办法有两个单独的分组列,一个用于mfr,一个用于year? 1 个回答 Voted Best Answer M-- 2024-04-05T02:50:54+08:002024-04-05T02:50:54+08:00 library(gt) library(dplyr) gtcars %>% mutate(mfr = factor(mfr, levels = unique(mfr))) %>% ## keep the original order arrange(mfr, year) %>% mutate(year = ifelse(row_number() == 1, as.character(year), ""), .by = c(mfr, year)) %>% mutate(mfr = ifelse(row_number() == 1, as.character(mfr), ""), .by = mfr) %>% select(mfr, year, everything()) %>% gt()
1 个回答