0

I'm trying to create a function, which creates n variables named automatically x1, x2, ..., xn. I have an approach, which works only through the console and not in a function (the runif creates an array of 1000 numbers between -1 and 1, which should be assigned to the variable):

for(i in 1:n) {
   assign(sprintf("x%d",i),runif(1000,min=-1,max=1))
}

When I use it in a function, no variables are created in the environment...

I tried to make something like this:

for(i in 1:n) {
   sprintf("x%d",i) <- runif(1000,min=-1,max=1))
}

But this returns the error "target of assignment expands to non-language object". Also tried to use as.name(), eval(), aso. But I didn't get the list of the variables x1 to xn in my environment through a function.

Could someone give me a hint? Thx a lot in advance!

lorny
  • 5
  • 2
  • I think the first example does create the variables. Have you checked your environment using `ls()` ? – phoxis May 10 '18 at 16:59
  • 2
    There is an answer here: https://stackoverflow.com/questions/9726705/assign-multiple-objects-to-globalenv-from-within-a-function you just need to specify the environment. But it comes with lots of commentary that says "Don't do this at home". Is there any reason you can't have the function return a list or vector? – steveLangsford May 10 '18 at 16:59
  • I'd recommend reading my answer to [How to make a list of data frames](https://stackoverflow.com/a/24376207/903061), I include a good bit of information of why this is Bad. Just use a list instead, make life easy. – Gregor Thomas May 10 '18 at 17:10
  • How to use a list in this case: `xlist = replicate(n, runif(1000, min = -1, max = 1), simplify = FALSE)`. Then use `x[[1]]`, `x[[5]]`, etc. – Gregor Thomas May 10 '18 at 17:13
  • Thx for all the useful comments! I will start to work with lists and matrixes. @phoxis I do work with RStudio, so I always have the environment visible in the specific frame. – lorny May 11 '18 at 06:51
  • In that case, it looks like you are generating this in a local scope. Better make a dataframe and return it. – phoxis May 11 '18 at 09:46

1 Answers1

0

Are you sure you want to create n different variable? The R way is to create a matrix or data.frame and play with it. If this is really 1000 variables that you need , then this question gives:

f<-function(n)for(i in 1:n){
    assign(sprintf("x%d",i),runif(1000,min=-1,max=1),envir = .GlobalEnv)
}
f(10)
x5

Try it online!

but you really should do:

f<-function(n){
x <- matrix(runif(1000*n),ncol=n)
colnames(x) <-paste0(rep("x",n),1:n)
x
}
f(10)

Try it online!

JayCe
  • 221
  • 3
  • 9
  • Thanks a lot, wasn't aware of the possibility to work with a matrix (I'm a R-noob :))... I have an assignment where I have to create 100 progressions of 1000 variables and add them to create a normal distribution... – lorny May 11 '18 at 06:46
  • Just checked this way as a function in RStudio...also didn't get the matrix in the environment. Maybe I have to add the `envir` argument? – lorny May 11 '18 at 08:13
  • You have to assign the result of the function. Try `m – JayCe May 11 '18 at 14:48
  • thx, no idea why I didn't get this one :) just had to put a simple return(...) and then the direct assignement! – lorny May 14 '18 at 09:58