Created
December 19, 2016 01:33
-
-
Save ZeccaLehn/61735aec7c2f69d6d0cde2d83afa4372 to your computer and use it in GitHub Desktop.
[R] Setting variables dynamically via strings within a loop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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