Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R, properly using the paste function

Tags:

r

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?

like image 473
MathLover Avatar asked May 23 '26 09:05

MathLover


1 Answers

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 = "") ) )

That being said, I would highly, highly recommend against this method.

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]])
}
like image 116
Ricardo Saporta Avatar answered May 26 '26 01:05

Ricardo Saporta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!