Skip to content

Instantly share code, notes, and snippets.

@yutannihilation
Last active April 2, 2017 00:09
Show Gist options
  • Select an option

  • Save yutannihilation/8b4c7638363222d5697161a69dde8070 to your computer and use it in GitHub Desktop.

Select an option

Save yutannihilation/8b4c7638363222d5697161a69dde8070 to your computer and use it in GitHub Desktop.
# 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