代码之家  ›  专栏  ›  技术社区  ›  bretauv

如何将组放在“gt”表的额外列中?

gt r
  •  2
  • bretauv  · 技术社区  · 2 年前

    在下表中,我如何删除左侧附加列中的组(不复制“A”和“B”),使表只有4行而不是6行?

    library(gt)
    library(dplyr, warn.conflicts = FALSE)
    
    test <- data.frame(
      group = c("A", "A", "B", "B"),
      year = c(1, 2, 1, 2),
      value = c(8:11)
    )
    
    test |> 
      group_by(group) |> 
      gt()
    

    enter image description here

    2 回复  |  直到 2 年前
        1
  •  2
  •   Aurèle    2 年前
    test |> 
      group_by(group) |> 
      gt() |>
      tab_options(row_group.as_column = TRUE)
    

    enter image description here

        2
  •  1
  •   zx8754    2 年前

    Aur¨le解决方案的替代方案。

    在不分组的情况下,我们可以按组列排序,然后将重复的行设置为空- ""

    test |> 
      arrange(group, year) |> 
      mutate(group = ifelse(duplicated(group), "", group)) |> 
      gt()
    

    enter image description here