I have a for loop where in each loop it executes a function. Firstly I'm loading .rda
files from the directory and then want to assign the loaded files (which are data frames) to spread
and ind
respectfully. How can I do this? Note that the variable names once get loaded are different in each loop i.e. when #1 is loaded it shows up as NS.ticker_list[j,2]
and #2 as Forward.rate.ticker_list[j,2]
.
for(j in 1:nrow(ticker_list)){
load(file = gsub(" ","",paste(ticker_list[j,2],"_NS.rda"))) #1
load(file = gsub(" ","",paste(ticker_list[j,2],"_Forward.rda"))) #2
p <- arima.auto.fun(spread, ind, maturity_list, lag = 1)
}
CodePudding user response:
You could load each file into a new environment, convert the environment into a list object, and then assign the first (and only) object in the lists to the spread
and ind
variables. You didn't provide a reproducible example, so the following is a rough guess. I've also removed what appears to be an unnecessary use of gsub
and replaced it with the simpler paste0
.
for(j in 1:nrow(ticker_list)){
NS_env <- new.env()
Forward_env <- new.env()
load(file = paste0(ticker_list[j,2],"_NS.rda"), envir = NS_env) #1
load(file = paste0(ticker_list[j,2],"_Forward.rda"), envir = Forward_env) #2
NS_list <- as.list(NS_env)
Forward_list <- as.list(Forward_env)
spread <- NS_list[[1]]
ind <- Forward_list[[1]]
p <- arima.auto.fun(spread, ind, maturity_list, lag = 1)
}