Census of Governments (COG)
Location, employment, and payroll for state and local (but not federal) government agencies in the U.S.
One record per agency, one per agency function, plus the government units master address file.
Complete enumeration of civilian employment in state and local governments in the 50 states + D.C.
The Annual Survey of Public Employment & Payroll becomes a census in years ending with 2 and 7.
Administered and financed by the US Census Bureau.
Please skim before you begin:
2022 Census of Governments, Survey of Public Employment & Payroll Methodology
This human-composed haiku or a bouquet of artificial intelligence-generated limericks
# courthouse steps wedding
# schools police fire water
# no fed mail invite
Download, Import, Preparation
Download, import, and stack the government units listing file:
library(readxl)
<- tempfile()
tf_gus
<- "https://www2.census.gov/programs-surveys/gus/datasets/2022/govt_units_2022.ZIP"
gus_url
download.file( gus_url , tf_gus , mode = 'wb' )
<- unzip( tf_gus , exdir = tempdir() )
unzipped_files_gus
<- grep( "\\.xlsx$" , unzipped_files_gus , value = TRUE )
xlsx_gus_fn
<- excel_sheets( xlsx_gus_fn )
xlsx_sheets
# read all sheets into a list of tibbles
<- lapply( xlsx_sheets , function( w ) read_excel( xlsx_gus_fn , sheet = w ) )
gus_tbl_list
# convert all tibbles to data.frame objects
<- lapply( gus_tbl_list , data.frame )
gus_df_list
# lowercase all column names
<-
gus_df_list lapply(
gus_df_list , function( w ){ names( w ) <- tolower( names( w ) ) ; w }
)
# add the excel tab source to each data.frame
for( i in seq( xlsx_sheets ) ) gus_df_list[[ i ]][ , 'source_tab' ] <- xlsx_sheets[ i ]
# determine which columns are in all tables
<- Reduce( intersect , lapply( gus_df_list , names ) )
column_intersect
# determine which columns are in some but not all tables
<- unique( unlist( lapply( gus_df_list , names ) ) )
column_union
# these columns will be discarded by stacking:
unique(
unlist(
lapply(
lapply( gus_df_list , names ) ,
function( w ) column_union[ !column_union %in% w ]
)
)
)
# stack all excel sheets, keeping only the columns that all tables have in common
<- Reduce( rbind , lapply( gus_df_list , function( w ) w[ column_intersect ] ) ) gus_df
Download and import the survey of public employment & payroll, one record per function (not per unit):
<- tempfile()
tf_apes
<-
apes_url paste0(
"https://www2.census.gov/programs-surveys/apes/datasets/" ,
"2022/2022%20COG-E%20Individual%20Unit%20Files.zip"
)
download.file( apes_url , tf_apes , mode = 'wb' )
<- unzip( tf_apes , exdir = tempdir() )
unzipped_files_apes
<- grep( "\\.xlsx$" , unzipped_files_apes , value = TRUE )
xlsx_apes_fn
<- read_excel( xlsx_apes_fn )
apes_tbl
<- data.frame( apes_tbl )
apes_df
names( apes_df ) <- tolower( names( apes_df ) )
Review the non-matching records between these two tables, then merge:
# all DEP School Districts and a third of Special Districts are not in the `apes_df`
table(
'census_id_gidid' ] %in% apes_df[ , 'individual.unit.id' ] ,
gus_df[ , 'source_tab' ] ,
gus_df[ , useNA = 'always'
)
# state governments are not in the `gus_df`
table(
'individual.unit.id' ] %in% gus_df[ , 'census_id_gidid' ] ,
apes_df[ , 'type.of.government' ] ,
apes_df[ , useNA = 'always'
)
# check for overlapping field names:
<- intersect( names( apes_df ) , names( gus_df ) ) )
( overlapping_names
# rename the state column in `gus_df` to state abbreviation
names( gus_df )[ names( gus_df ) == 'state' ] <- 'stateab'
<-
double_df merge(
apes_df ,
gus_df ,by.x = 'individual.unit.id' ,
by.y = 'census_id_gidid' ,
all.x = TRUE
)
stopifnot( nrow( double_df ) == nrow( apes_df ) )
# replace dots with underscores
names( double_df ) <- gsub( "\\." , "_" , names( double_df ) )
Keep either the one record per agency rows or the one record per function rows:
# `Total - All Government Employment Functions` records sum to the same as all other records:
with( double_df , tapply( full_time_employees , grepl( "Total" , government_function ) , sum ) )
with( double_df , tapply( part_time_payroll , grepl( "Total" , government_function ) , sum ) )
# keep one record per government function (multiple records per agency):
<- subset( double_df , !grepl( "Total" , government_function ) )
cog_df
# keep one record per government agency:
# cog_df <- subset( double_df , grepl( "Total" , government_function ) )
Analysis Examples with base R
Unweighted Counts
Count the unweighted number of records in the table, overall and by groups:
nrow( cog_df )
table( cog_df[ , "type_of_government" ] , useNA = "always" )
Descriptive Statistics
Calculate the mean (average) of a linear variable, overall and by groups:
mean( cog_df[ , "full_time_employees" ] )
tapply(
"full_time_employees" ] ,
cog_df[ , "type_of_government" ] ,
cog_df[ ,
mean )
Calculate the distribution of a categorical variable, overall and by groups:
prop.table( table( cog_df[ , "census_region" ] ) )
prop.table(
table( cog_df[ , c( "census_region" , "type_of_government" ) ] ) ,
margin = 2
)
Calculate the sum of a linear variable, overall and by groups:
sum( cog_df[ , "full_time_employees" ] )
tapply(
"full_time_employees" ] ,
cog_df[ , "type_of_government" ] ,
cog_df[ ,
sum )
Calculate the median (50th percentile) of a linear variable, overall and by groups:
quantile( cog_df[ , "full_time_employees" ] , 0.5 )
tapply(
"full_time_employees" ] ,
cog_df[ , "type_of_government" ] ,
cog_df[ ,
quantile ,0.5
)
Subsetting
Limit your data.frame
to Elementary, Secondary, Higher, and Other Educational Government Agencies:
<- subset( cog_df , grepl( 'Education' , government_function ) ) sub_cog_df
Calculate the mean (average) of this subset:
mean( sub_cog_df[ , "full_time_employees" ] )
Measures of Uncertainty
Calculate the variance, overall and by groups:
var( cog_df[ , "full_time_employees" ] )
tapply(
"full_time_employees" ] ,
cog_df[ , "type_of_government" ] ,
cog_df[ ,
var )
Regression Models and Tests of Association
Perform a t-test:
t.test( full_time_employees ~ any_full_time_employees , cog_df )
Perform a chi-squared test of association:
<- table( cog_df[ , c( "any_full_time_employees" , "census_region" ) ] )
this_table
chisq.test( this_table )
Perform a generalized linear model:
<-
glm_result glm(
~ any_full_time_employees + census_region ,
full_time_employees data = cog_df
)
summary( glm_result )
Replication Example
This example matches excel cell “C17” of Employment & Payroll Data by State and by Function:
<- subset( cog_df , government_function == 'Financial Administration' )
financial_admin_df
stopifnot( sum( financial_admin_df[ , 'full_time_employees' ] ) == 404228 )
Analysis Examples with dplyr
The R dplyr
library offers an alternative grammar of data manipulation to base R and SQL syntax. dplyr offers many verbs, such as summarize
, group_by
, and mutate
, the convenience of pipe-able functions, and the tidyverse
style of non-standard evaluation. This vignette details the available features. As a starting point for COG users, this code replicates previously-presented examples:
library(dplyr)
<- as_tibble( cog_df ) cog_tbl
Calculate the mean (average) of a linear variable, overall and by groups:
%>%
cog_tbl summarize( mean = mean( full_time_employees ) )
%>%
cog_tbl group_by( type_of_government ) %>%
summarize( mean = mean( full_time_employees ) )
Analysis Examples with data.table
The R data.table
library provides a high-performance version of base R’s data.frame with syntax and feature enhancements for ease of use, convenience and programming speed. data.table offers concise syntax: fast to type, fast to read, fast speed, memory efficiency, a careful API lifecycle management, an active community, and a rich set of features. This vignette details the available features. As a starting point for COG users, this code replicates previously-presented examples:
library(data.table)
<- data.table( cog_df ) cog_dt
Calculate the mean (average) of a linear variable, overall and by groups:
mean( full_time_employees ) ]
cog_dt[ ,
mean( full_time_employees ) , by = type_of_government ] cog_dt[ ,
Analysis Examples with duckdb
The R duckdb
library provides an embedded analytical data management system with support for the Structured Query Language (SQL). duckdb offers a simple, feature-rich, fast, and free SQL OLAP management system. This vignette details the available features. As a starting point for COG users, this code replicates previously-presented examples:
library(duckdb)
<- dbConnect( duckdb::duckdb() , dbdir = 'my-db.duckdb' )
con dbWriteTable( con , 'cog' , cog_df )
Calculate the mean (average) of a linear variable, overall and by groups:
dbGetQuery( con , 'SELECT AVG( full_time_employees ) FROM cog' )
dbGetQuery(
con ,'SELECT
type_of_government ,
AVG( full_time_employees )
FROM
cog
GROUP BY
type_of_government'
)