如何根据变量生成单独的 GT 表

How to produce separate GT tables based on a variable

首先是示例数据、其中一个变量的架构,以及我用来操作它的代码。以及想要的结果。经过检查我意识到第四个元素是不正确的,因此需要对此进行澄清。问题是我如何让 R 从中生成两个 gt 表。实际上,我有 17 个区域,但对于这个小集合只有 2 个。我如何让 R 生成 2 个代表该区域(001 或 003)的 gt 表。

  library(readxl)
  library(dplyr)
  library(data.table)
  library(odbc)
  library(DBI)
  library(stringr)

  firm <- c("f1","f2","f3","f4","f5","f6","f7","f8","f9","f10","f11","f12")
  employment <- c(1,50,90,249,499,115,145,261,210,874,1140,45)
  small <- c(1,1,1,3,4,2,2,4,3,NA,NA,1)
  area <-c(001,001,001,001,001,001,003,003,003,003,003,003)

  smbtest <- data.frame(firm,employment,small,area)

  smbsummary2<-smbtest %>% 
  select(firm, employment, small, area) %>%
  group_by(area,small) %>%
  summarise(employment = sum(employment), worksites = n(), 
        .groups = 'drop') %>% 
  mutate(employment = cumsum(employment),
     worksites = cumsum(worksites))

  Schema:
  smb 1 = employing between 0 and 100
  smb 2 = employing between 0 and 150
  smb 3 = employing between 0 and 250
  smb 4 = employing between 0 and 500


  Desired Result (these would be tables as made by gt)
 

 Area    Small     Employment      Worksites
 001      1           141             3
 001      2           115             1
 ....(and on to small 4)

 Area    Small     Employment      Worksites
 003       1           45             1
 003       2           145            1     
  ....(and on to small 4)

 


 

您要为每个 area 单独 gt table 吗?试试这个:

list_gt <- lapply(split(smbsummary2, smbsummary2$area), gt::gt)

然后您可以使用 list_gt[[1]]list_gt[[2]] 等访问每个人 gt table。