Skip to content
Prev 257836 / 398502 Next

Assigning variables using a loop

Hi Nick,

It is typically not a good idea to write functions that automatically
assign variables to the global environment (makes it easy to overwrite
something valuable without knowing you are overwriting it), but if
that is really the best choice for your situation, I would do
something like (inside your function):

# note, if global assignment not needed, just return this
mydata <- list(x = 1, y = 2, z = 3)
lapply(names(mydata), function(x) {
  assign(x, mydata[[x]], pos = .GlobalEnv)
})

HTH,

Josh
On Sat, Apr 23, 2011 at 10:51 AM, Nick Mosely <mosely at uw.edu> wrote: