简体   繁体   English

如何使列名称/数据框名称成为包含变量的字符串(R)

[英]How to make column names/ dataframe names a string with a variable in it (R)

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. 我正在使用acs软件包从美国社区调查中提取数据,以更新信息图表网站。 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 . 我希望通过输入this.year的当前年份并让代码更新过去2年的数据xy来每年运行代码。

If this.year is 2015, x is 2014, xs is 14, y is 2013, ys is 13. The end result I want is (for y) the data frame name tpop_13 with the column name DM_TPOP_13 . 如果this.year是2015, x是2014, xs为14, y是2013, ys是13最终的结果我想是(对于y)处的数据帧名称tpop_13与列名DM_TPOP_13 (for x) the data frame name tpop_14 with the column name DM_TPOP_14 . (对于x而言)数据框名称tpop_14和列名称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 . 该代码是正确抽取所需要的数据,但这个代码返回(对于y)处的数据帧名称tpop_y.s与列名DM_TPOP_y.s (for x) the data frame name tpop_x.s with the column name DM_TPOP_x.s . (对于x而言)数据框名称tpop_x.s和列名称DM_TPOP_x.s I tried searching for similar questions and found this one: How to print R variables in middle of String 我尝试搜索类似的问题,并发现了以下问题: 如何在字符串中间打印R变量

I tried applying the answer by using the quotes \\"',xs,'\\" to solve my problem, but it doesn't work. 我尝试通过使用引号\\"',xs,'\\"来应用答案来解决我的问题,但是它不起作用。 The code returns (for x) the column name DM_TPOP_...xs.. . 代码返回(对于x)列名DM_TPOP_...xs.. I understand that R does not evaluate any expression within the quotes - it just prints the string you defined. 我知道R不会对引号内的任何表达式求值-它只会打印您定义的字符串。 But how can you get around this problem so that there can be a variable in a string? 但是如何解决这个问题,以便字符串中可以有一个变量?

Help would be greatly appreciated! 帮助将不胜感激!

You can use functions like paste , paste0 , and sprintf to construct strings from string constants and variables. 您可以使用pastepaste0sprintf的函数根据字符串常量和变量构造字符串。 There is also functionality in the gsubfn package to do Perl like string interpolation. gsubfn软件包中还有功能可以像字符串插值一样执行Perl。

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. 工作方法如下:将数据存储在列表kk ,以便第一个元素kk[[1]]kk[["tpop_13"]]给出2013年的数据,第二个元素kk[[2]]kk[["tpop_12"]]给出2012年的数据,并根据您的建议提供每个元素的名称。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM