Skip to content
Prev 286617 / 398502 Next

macro function

Hi,

There is not anything really like macro functions in R (and honestly,
that is probably a good thing).  Most the times I have seen people
generating thousands of variables (vectors in your case) it is due to
a lack of understanding how lists can be utilized to simplify code.
If you give us some context on what all these vectors will be used
for, we may be able to suggest an easier way to accomplish your goals.

If you must create vectors:

sapply(1:3003, function(i) {
  assign(paste("r", i, sep = ''), rnorm(1), envir = .GlobalEnv)
})

or with a for loop:

for (i in 1:3003) {
  assign(paste("r", i, sep = ''), rnorm(1), envir = .GlobalEnv)
}

HTH,

Josh
On Tue, Feb 28, 2012 at 12:14 AM, mrzung <mrzung46 at gmail.com> wrote: