Search code examples
rdplyrgt

How to produce separate GT tables based on a variable


First is the sample data, schema for one of the variables, and the code that I am using to manipulate it. along with the desired result. I realize upon inspection that the fourth element is not correct and so need clarification on this. The question is how I would have R produce two gt tables from this. In reality, I have 17 areas but for this small set there are only 2. How would I have R produce 2 gt tables that represent the area (001 or 003).

  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)

 


 

Solution

  • Do you want a separate gt table for each area ? Try this :

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

    You can then access to each individual gt table with list_gt[[1]], list_gt[[2]] and so on.