0

我想从 R 中的表格制作一张漂亮的表格。这是从因子变量创建的表格:

a=factor(sample(1:4, 10000, replace=TRUE))

b=factor(sample(c(0,1,NA), 10000, replace=TRUE))

table(a,b,exclude=NULL)

这是 table 命令的输出:

  b
a     0   1 <NA>
  1 855 824  851
  2 802 843  870
  3 821 868  855
  4 795 786  830

我想知道如何在这个表格中添加标题和格式来制作一个漂亮的表格,比如使用 gt 或其他类似的包

4

1 回答 1

1
c <- table(a,b,exclude=NULL)

library(dplyr); library(tidyr); library(gt)

c %>%
  tibble::as_tibble() %>%
  tidyr::pivot_wider(names_from = b, values_from = n) %>%
  gt::gt() %>%
  gt::tab_header(
    title = "How do A and B coincide?",
    subtitle = "Such a great table"
  ) %>% gt::tab_spanner(
    label = "b",
    columns = 2:4
  )

在此处输入图像描述

于 2021-04-22T16:03:30.543 回答