Reputation: 75
This loop properly creates 13 df's named bond1,...,bond13 and assigns them the values from function1. Now I need to create 13 more DF's named spread1, ..., spread13 using function2 and two other df's. One of these is fixed for all 13 spreads (DF_B) but for spreadi I need bondi and the second line in the code gives me an error of the following: "Loading required package: tcltk Error in sqliteExecStatement(con, statement, bind.data) : RS-DBI driver: (error in statement: no such table: bond)"
for(i in 1:13)
{
assign(paste("bond", i, sep = ""), function1(DF_A))
assign(paste("spread", i, sep = ""), function2(DF_B, paste("bond", i, sep = "")))
}
What is the proper way to do this?
Upvotes: 3
Views: 208
Reputation: 121608
I would recommend to avoid using a for
loop here and avoid allocating memory in advance. Using replicate
for example and keeping all your objects in a single list is the R-way to do things.
replicate(13,{
bond <- function1(DF_A) ## Note here all your bonds are the same...
spread <- function2(DF_B, bond)
list(bond=bond,spread=spread)
},simplify=FALSE)
Upvotes: 2
Reputation: 55420
In function2 you are passing a string as the argument, not your actual data object.
Wrap get(.)
around the paste function and you are all set:
function2(DF_B, get( paste("bond", i, sep = "") ) )
Instead, use a list
bond <- vector("list", 13)
spread <- vector("list", 13)
for(i in 1:13)
{
bond[[i]] <- function1(DF_A)
spread[[i]] <- function2(DF_B, bond[[i]])
}
Upvotes: 9