Search code examples
rloopsindices

loop in R - Variable with Indices


Hi my Code is like that

for(k in 1:length(t(trd))) ... { names(Germany) <- c("ISIN","MATURITYDATE","ISSUEDATE","COUPONRATE","PRICE","ACCRUED","CASHFLOWS","TODAY")
}

My Code compute all the components of the list Germany. For each "k" I want to safe my list Germany into BondDayk.

As result I Need BondDay1,.., BondDay150 and each of them are lists.

I Need a algo which does:

BondDay1 <- list("Germany") BondDay2 <- list("Germany") BondDay3 <- list("Germany") ....

"Germany" is for each "k" a different dataset.

Thank you


Solution

  • It's clearer. Use:

    for (i in 1:150) {
      eval(parse(text = sprintf("BondDay%d <- list(germany)", i)), envir = -1)
    }
    

    If you put that in a function, use envir = -2 instead.