I work for a small nonprofit trying to look at census data for guiding our work. since I have to look at a bunch of different census tables looking for trends I would love to automate this since the task is the same I am just changing table names.
Here is what I have so far:
Variables block
#table specific variables
importpath <-paste("C:/Users/DataNerd/OneDrive/",
"censusdata/",
sep = "")
#columns to keep from larger table
focuscolumns <- c("GEO.id2", "HC01_EST_VC01", "HC02_EST_VC01")
#years to focus on from the acs data
acsyear <- c("15", "14", "13", "12")
#Name of the ACS table
acstablename <- "S1701"
#data naming variables
datanames <- c("poverty", "rate", etc...)
So the variables block above is the only block of code I want to change when I go from examining a census table on Poverty Rate to one on substandard housing.
Using those variables I can get pretty far.
#create the table name
tablefile <- paste("ACS_", acsyear[1], "_5YR_", acstablename, sep = "")
#import data
tableA<- read_csv(paste(importpath,
tablefile,
"/",
tablefile,
".csv",
sep = ""))
ACS15 <- subset(tableA, select = focuscolumns)
tablefile <- paste("ACS_", acsyear[2], "_5YR_", acstablename, sep = "")
tableA<- read_csv(paste(importpath,
tablefile,
"/",
tablefile,
".csv",
sep = ""))
ACS14 <- subset(tableA, select = focuscolumns)
The issue I have concerns creating the names and functions using the variables block. To get poverty rate I need to divide one column by another column in my focuscolumns variable.
Here is what I thought would work.
paste0('ACS', acsyear[1], datanames[1]) <- mutate( paste0("ACS", acsyear[1],
paste0("ACS",acsyear[1],datanames[1],datanames[2]) = focuscolumns[3]/ focuscolumns[2]
))
Through the magic of R I thought that code should evaluate down to:
ACS15poverty <- mutate(ACS15,
ACS15povertyrate = HC02_EST_VC01/HC01_EST_VC01)
It does not and I am not sure how to R to evaluate it in the way I was hoping for.
Any help would be appreciated.