Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中的循环中运行不同的数据集_R_Database_Loops_Variables_For Loop - Fatal编程技术网

在R中的循环中运行不同的数据集

在R中的循环中运行不同的数据集,r,database,loops,variables,for-loop,R,Database,Loops,Variables,For Loop,我试图通过一个循环运行一个数据集的多年,具体来说是2009年到2014年,并重命名变量以反映年份。例如,我正在从2009年开始加载consolidated,并尝试将$AGE09重命名为$AGE。澄清一下:我希望能够在2010年、2011年等年完成这项工作——我希望能够运行consolidated.2009$AGE关于您的问题的第一点评论: 当您使用i==2009调用rx_files[[i]]时,您正在查找该列表中的第2009个元素,我怀疑该列表从第1年开始,因此这样做可能无法获得2009年 当您

我试图通过一个循环运行一个数据集的多年,具体来说是2009年到2014年,并重命名变量以反映年份。例如,我正在从2009年开始加载
consolidated
,并尝试将
$AGE09
重命名为
$AGE
。澄清一下:我希望能够在2010年、2011年等年完成这项工作——我希望能够运行
consolidated.2009$AGE关于您的问题的第一点评论:
当您使用i==2009调用rx_files[[i]]时,您正在查找该列表中的第2009个元素,我怀疑该列表从第1年开始,因此这样做可能无法获得2009年

当您执行rx。[[i]]时,似乎您正在尝试连接一个名称,例如rx.2009。它不是这样工作的,[[用于列表rx。不是列表

请试试这个,让我知道如果这是你想要的,我可以编辑解释

library(data.table) # to use the function setnames, execute install.packages("data.table") if you don't have it
rx_files <- as.list(dir(pattern="* - rx.rda"))
consolidated_files <- as.list(dir(pattern="* - consolidated.rda"))
#supposing all these files start with a clean year (4 numeric characters) and that there's only one file per year and filetype
years_rx <- as.numeric(substr(rx_files,1,4)
years_cf <- as.numeric(substr(consolidated_files,1,4)
for(i in c(2009:2014)){                                                             
  load(rx_files[[which(years_rx == i)]]) # not used actually, why do you load it ?
  load(consolidated_files[[which(years_cf == i)]])
  eval(parse(text=sprintf("setnames(consolidated.%s,'AGE%sX','AGE')",i,substr(i,3,4))))
}
library(data.table)#要使用函数setnames,请执行install.packages(“data.table”)(如果没有)

rx_文件嗨,谢谢,这非常有帮助。代码现在正在运行,并按照希望的那样重命名变量。为了回答你之前的观点,我编写了一些代码,使列表成为列表中的2009-2014元素,以简化事情,但认为这偏离了我问题的重点。非常好,如果你需要我解释一些行,请告诉我S,或者如果它足够好,然后考虑验证答案:
library(data.table) # to use the function setnames, execute install.packages("data.table") if you don't have it
rx_files <- as.list(dir(pattern="* - rx.rda"))
consolidated_files <- as.list(dir(pattern="* - consolidated.rda"))
#supposing all these files start with a clean year (4 numeric characters) and that there's only one file per year and filetype
years_rx <- as.numeric(substr(rx_files,1,4)
years_cf <- as.numeric(substr(consolidated_files,1,4)
for(i in c(2009:2014)){                                                             
  load(rx_files[[which(years_rx == i)]]) # not used actually, why do you load it ?
  load(consolidated_files[[which(years_cf == i)]])
  eval(parse(text=sprintf("setnames(consolidated.%s,'AGE%sX','AGE')",i,substr(i,3,4))))
}