1
this.year<-2014 
x<-this.year-1 
y<-this.year-2
x.s<-x-2000 
y.s<-y-2000
tpop_y.s<-acs.fetch(endyear=x,span=1,geography=mystates,variable="B01003_001", col.names="DM_TPOP_x.s")  
tpop_x.s<-acs.fetch(endyear=y,span=1,geography=mystates,variable="B01003_001",col.names="DM_TPOP_y.s")

I'm using the package acs to pull out data from the American community survey to update an infographics website. I hope to run the code every year by inputing the current year in this.year and having the code update data for the past 2 years, x and y.

If this.year is 2015, x is 2014, x.s is 14, y is 2013, y.s is 13. The end result I want is (for y) the data frame name tpop_13 with the column name DM_TPOP_13. (for x) the data frame name tpop_14 with the column name DM_TPOP_14.

The code is pulling the desired data correctly, but this code returns (for y) the data frame name tpop_y.s with the column name DM_TPOP_y.s. (for x) the data frame name tpop_x.s with the column name DM_TPOP_x.s. I tried searching for similar questions and found this one: How to print R variables in middle of String

I tried applying the answer by using the quotes \"',x.s,'\" to solve my problem, but it doesn't work. The code returns (for x) the column name DM_TPOP_...x.s.... I understand that R does not evaluate any expression within the quotes - it just prints the string you defined. But how can you get around this problem so that there can be a variable in a string?

Help would be greatly appreciated!

Community
  • 1
  • 1
Zi Xin Lee
  • 43
  • 1
  • 1
  • 7
  • Do you need such a complicated example? I've never seen `acs.fetch` before and doubt that it is important to a question of strings and variable names... – Frank May 27 '15 at 15:56
  • `acs.fetch` is found in the package `acs` that is not a commonly used package. I'm sorry if the example seems complicated. It is just how the arguments in acs.fetch works. – Zi Xin Lee May 27 '15 at 15:58
  • The `assign` function may help, see here: http://stackoverflow.com/questions/5510966/create-a-variable-name-with-paste-in-r – Marta Cz-C May 27 '15 at 15:59
  • I have posted an answer. Let me know if it works for you. – user227710 May 27 '15 at 16:27

2 Answers2

0

You can use functions like paste, paste0, and sprintf to construct strings from string constants and variables. There is also functionality in the gsubfn package to do Perl like string interpolation.

Greg Snow
  • 48,497
  • 6
  • 83
  • 110
0

Here is how you can work: store the data in the list kk, so that first element ,kk[[1]] or kk[["tpop_13"]], gives the data for 2013 and second element,kk[[2]] or kk[["tpop_12"]] gives the data for 2012, with the name of each element as you suggested.

wa=geo.make(state="WA")
kk<-lapply(c(x,y),function(i){
  acs.fetch(endyear=i,span=1,geography=wa,variable="B01003_001", col.names=paste0("DM_TPOP_",i-2000)) 
   })


names(kk)<-paste0("tpop_",c(x-2000,y-2000))
   kk 
        $tpop_13
    ACS DATA: 
     2013 ;
      Estimates w/90% confidence intervals;
      for different intervals, see confint()
               DM_TPOP_13   
    Washington 6971406 +/- 0

    $tpop_12
    ACS DATA: 
     2012 ;
      Estimates w/90% confidence intervals;
      for different intervals, see confint()
               DM_TPOP_12   
    Washington 6897012 +/- 0
user227710
  • 3,164
  • 18
  • 35