Merge cells in a gt table

I'd like to create a table that "merges" cells within a column. Let's say I have data like this:

(tib <- tibble::tibble(group = rep(1:2, each = 2), 
                               value = rep(1:2, times = 2))
                               )

I can pass tib to gt::gt to get a table:

library(magrittr)
tib %>% 
  gt::gt()

And I get a table with four rows and 2 columns. That is, there are 4 rows in each of the two columns. i'd like to have a table where the number of rows differs between the two columns. That is, I'd like to have a table that has two rows in the first column and four in the second column. In such a table, each group value would be present exactly once. It looks (almost) like this markdown table:

group value
1 1
2
2 1
2

The only difference is that I want the 4 cells in column group to "merge" into 2 cells.

I've seen that one can use groups - grouping by, in my case, group - to almost get what I want.

tib %>%
  dplyr::group_by(group) %>%
  gt::gt()

It seems that the default appearance, when using the above code, is to present the table as a single column, where the group labels (ie, levels of group, here) are in the same column as the values of value.

I'd like to move the group labels to their own column in the resulting table. I appreciate any suggestions for doing this.

Thank you.

2 Likes

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.