Last active
April 2, 2017 00:09
-
-
Save yutannihilation/8b4c7638363222d5697161a69dde8070 to your computer and use it in GitHub Desktop.
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
| # get all variable names | |
| varnames <- paste0("Var", 1:50) | |
| # get all variable values | |
| varvalues <- sapply(varnames, function(x) get(x, envir = .GlobalEnv)) | |
| # calculate new variable values | |
| varvalues_new <- varvalues * 1:50 | |
| # assign to the original variables | |
| for(i in 1:50) { | |
| assign(x = varnames[i], | |
| value = varvalues_new[i], | |
| envir = .GlobalEnv) | |
| } | |
| # lapply version: | |
| # | |
| # lapply(1:50, function(i) assign(x = varnames[i], | |
| # value = varvalues_new[i], | |
| # envir = .GlobalEnv)) | |
| # purrr version: | |
| # | |
| # purrr::walk2(varnames, varvalues_new, | |
| # ~assign(x = .x, value = .y, envir = .GlobalEnv)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment