添加新字段后创建 gt table 的问题

Issues with creating gt table after adding new fields

以下是示例数据、包和操作。第三部分和第四部分是核心问题所在

此处的目标是生成一个 table,其中包含按 smb 类别和时间段划分的就业。如果我省略第 3 部分和 col_order 项,效果很好。所以问题是我如何创建第 3 部分中的项目并创建一个没有这些字段并且也不偏移 table 中的数据的 gt table。我试过将 "%>% select (-"empprevyear",-"emprevyearpp",-"empprevyearpct") 嵌入其中,但仍然把事情搞砸了。另外,我试过 col_order (http://www.sthda.com/english/wiki/reordering-data-frame-columns-in-r) 但仍然没有成功。

简而言之,如果您 运行 除了第 3 部分之外的所有代码,它都会产生所需的结果。问题是如何在添加额外的计算字段后得到相同的结果?

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

employment <- c(1,45,125,130,165,260,600,2,46,127,132,167,265,601,50,61,110,121,170,305,55,603,66,112,123,172,310,604)
small <- c(1,1,2,2,3,4,NA,1,1,2,2,3,4,NA,1,1,2,2,3,4,NA,1,1,2,2,3,4,NA)
area <-c(001,001,001,001,001,001,001,001,001,001,001,001,001,001,003,003,003,003,003,003,003,003,003,003,003,003,003,003)
 year<-c(2020,2020,2020,2020,2020,2020,2020,2020,2020,2020,2020,2020,2020,2020,2020,2020,2020,2020,2020,2020,2020,2020,2020,2020,2020,2020,2020,2020)
qtr <-c(1,1,1,1,1,1,1,2,2,2,2,2,2,2,1,1,1,1,1,1,1,2,2,2,2,2,2,2)

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

 smbtest$smb <-0

   smbtest <- smbtest %>% mutate(smb = case_when(employment >=0 & employment <100 ~ "1",employment >=0 & employment <150 ~ "2",employment >=0 & employment <250 ~ "3", employment >=0 & employment <500 ~ "4"))


 smbsummary2<-smbtest %>% 
 mutate(period = paste0(year,"q",qtr)) %>%
 select(area,period,employment,smb) %>%
 group_by(area,period,smb) %>%
 summarise(employment = sum(employment), worksites = n(), 
        .groups = 'drop_last') %>% 
 mutate(employment = cumsum(employment),
     worksites = cumsum(worksites))

 ### part 3 (outlined above)
 smbsummary2<- smbsummary2%>%
 group_by(area,smb)%>%
 mutate(empprevyear=lag(employment),
     empprevyearpp=employment-empprevyear,
     empprevyearpct=((employment/empprevyear)-1), 
  empprevyearpct=scales::percent(empprevyearpct,accuracy = 0.01)
 )

 ###part 4
 smblonger2<-smbsummary2 %>%
 ungroup() %>%
 pivot_longer(cols = employment:worksites, names_to = "measure", values_to = "value") %>%
 group_by(area,measure) %>%
 pivot_wider(names_from = period, values_from = value)%>%gt()

答案是我需要在第 4 部分的第一行放置一个 select 语句。

select("area","period","smb","employment","worksites")%>%