0

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.

  • A couple thoughts: (a) you should look at `?list.files` so that instead of building filepaths from scratch, you just read the paths of files that are there (and maybe filter them as necessary); (b) Pasting together variable names is possible, but usually unreadable and leads to bugs. I'd suggest reading [How to make a list of data frames](https://stackoverflow.com/a/24376207/903061) and think about using a list instead. (c) Strings are not object names - if you *do* continue on this route (rather than using lists as I suggest in (b)), you should... – Gregor Thomas Jun 12 '17 at 16:46
  • read about `?assign` and `?get`, and [this answer is good too](https://stackoverflow.com/q/18222286/903061), [and this one](https://stackoverflow.com/q/9083907/903061), which covers you pretty well in base R, but if you're using `dplyr` the Non-Standard Evaluation makes things trickier, so also read [use dynamic variable names in dplyr](https://stackoverflow.com/q/26003574/903061) and the `dplyr` vignette on [programming with dplyr](https://cran.r-project.org/web/packages/dplyr/vignettes/programming.html). – Gregor Thomas Jun 12 '17 at 16:46
  • Overall, I would propose closing this question as a dupe of one of the question linked above, probably the [dynamic variable names in `dplyr`](https://stackoverflow.com/q/26003574/903061) one. This question isn't asking anything that isn't covered on the other answers, and it's not reproducible (since we don't have your files) so it would be difficult to answer without heavy edits. – Gregor Thomas Jun 12 '17 at 16:48

0 Answers0