[R] Assigning variables using a loop

Nick Mosely mosely at uw.edu
Sat Apr 23 19:51:22 CEST 2011

Hello R-world,

I have multiple variables that have been generated within a function.
I would like to assign them each to the Global Environment. I've tried
the following:

x = 1
y = 2
z = 3
for (i in c(x,y,z)) {
[1] 3

Obviously, the problem is that the code is assigning numbers to the
the new variable i. I tried to get cute using paste:

> x = 1
> y = 2
> z = 3
> for (i in c(x,y,z)) {
+   assign(paste(i),i,pos=.GlobalEnv)
+ }

But paste enters "1" when i is x, rather than the desired "x". Does
anyone know of a solution to this problem?



More information about the R-help mailing list