Skip to content

Instantly share code, notes, and snippets.

@ZeccaLehn
Created December 19, 2016 01:33
Show Gist options
  • Save ZeccaLehn/61735aec7c2f69d6d0cde2d83afa4372 to your computer and use it in GitHub Desktop.
Save ZeccaLehn/61735aec7c2f69d6d0cde2d83afa4372 to your computer and use it in GitHub Desktop.
[R] Setting variables dynamically via strings within a loop
# Static variable setting
eval(parse(text="adder1 <- 1 + 3; adder2 <- 1 + 4"))
adder1 # [1] 4
adder2 # [1] 5
# Dynamic variable setting
for(i in 1:2){
print(paste0("adder", i, " <- ", i + 1))
eval(parse(text = paste0("adder", i, " <-", i + 1)))
}
adder1 # [1] "adder1 <- 2"
adder2 # [1] "adder2 <- 3"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment