0

I have code that generates a string that i want to use as a variable name later in my script. Is there a function, or easy way to accomplish this? I know this is such base question... any help is still very much appreciated.

Var1 <- "NewVarName"

# Need to transform the character string inside Var1 into this new variable 
  name

NewVarName <- 1 + 2
Joe
  • 349
  • 1
  • 2
  • 11
  • 3
    Maybe you need `assign` , `assign(Var1, 1+2)` – Ronak Shah Oct 15 '18 at 01:16
  • 2
    Ronak has the correct solution, but please reconsider your process. It's usually better to manage names in a `list` than it is to modify the global environment in a way that is going to appear as a non-explicit side-effect (the newly assigned name is opaque to anyone reading the code, and said opacity is less likely to cause issues in a list — where computed-name-based object assignment is an established idiom). – hrbrmstr Oct 15 '18 at 01:23
  • @ Ronak Shah Thank you for your help. – Joe Oct 15 '18 at 23:50
  • @ hrbrmstr: i can see your point, it becomes difficult to determine what the name of the new variable actually is by looking at the code. Can you recommend how to use a list if you only are working with one name at a time? Thank you for your time and interest. – Joe Oct 15 '18 at 23:52

0 Answers0