There is gt
table Current Table
, how can I change to Wished Table1
and Wished Table2
?
(There are separated question) .Refer to attached image. Thanks!
library(tidyverse)
library(gt)
diamonds %>% head(5) %>% group_by(color) %>%
summarise(x=list(x),y=list(y)) %>% gt()
Unlist the columns before paste collapse:
Example input with list columns
library(gt)
library(dplyr)
d <- ggplot2::diamonds %>%
head(5) %>%
group_by(color) %>%
summarise(x = list(x), y = list(y))
d
# # A tibble: 3 × 3
# color x y
# <ord> <list> <list>
# 1 E <dbl [3]> <dbl [3]>
# 2 I <dbl [1]> <dbl [1]>
# 3 J <dbl [1]> <dbl [1]>
Group by colour, then paste with collapse with "linebreak"
and ungroup before gt:
# Table 1
d %>%
group_by(color) %>%
mutate(x = paste(unlist(x), collapse = "<br>"),
y = paste(unlist(y), collapse = "<br>")) %>%
ungroup() %>%
gt() %>%
fmt_markdown(columns = c(x, y))
Same again but collapse on "_"
:
# Table 2
d %>%
group_by(color) %>%
mutate(x = paste(unlist(x), collapse = "_"),
y = paste(unlist(y), collapse = "_")) %>%
ungroup() %>%
gt()