1

I am trying to create 1000 variables, which I want to name with the index number. I don't know how to create these new variables.

for(i in 1:1000) {
  Ui <- rnorm(200,0,1)
}
  • 8
    Not only is this a bad idea, I'm quite sure it has been asked before. You should search before posting questions. – IRTFM Apr 17 '19 at 04:06
  • Try with `assign` – yarnabrina Apr 17 '19 at 04:09
  • 1
    Also: `rnorm(200, 0, 1)` generates 200 (not 1) normally distributed variables. So what you're doing (or rather attempt to do) in your `for` loop is to generate 200 random variables 1000 times. – Maurits Evers Apr 17 '19 at 04:13

3 Answers3

4

This is a common sort of thing that people want to do, especially when they are coming from other programming languages. However, there are better ways to accomplish the same thing, and you should not follow recommendations to use assign; that is bad advice that you will likely regret later on.

The way we do this sort of thing in R is to use lists, specifically named lists:

x <- replicate(1000,rnorm(200,0,1),simplify = FALSE)
x <- setNames(x,paste0("A",seq_along(x)))

Now x is a named list of length 1000, each element is a vector of length 200 from a normal(0,1) distribution.

You can refer to each one via x[[1]] or x[["A1"]] as needed. Additionally, since they are all in the same object, you can operate on the easily as a group using tools like lapply.

Pretty much any time you find yourself wanting to create a sequence of objects with similar names, that should be a signal to you that you should be using a list instead.

joran
  • 169,992
  • 32
  • 429
  • 468
3

There is no point in cluttering your environment with so many variables, try to store them in a named list instead

l1 <- setNames(lapply(1:5, function(x) rnorm(5)), paste0("A", 1:5))
l1

#$A1
#[1]  0.4951453 -1.4278665  0.5680115  0.3537730 -0.7757363

#$A2
#[1] -0.11096037  0.05958700  0.02578168  1.00591996  0.54852030

#$A3
#[1]  0.1058318  0.6988443 -0.8213525 -0.1072289  0.8757669

#$A4
#[1] -0.6629634  0.8321713 -0.3073465 -0.2645550 -1.0064132

#$A5
#[1]  2.2191246  0.2054360 -0.1768357  1.6875302 -1.1495807

Now you can access individual list element as

l1[["A1"]]
#[1] 0.4951453 -1.4278665  0.5680115  0.3537730 -0.7757363

Moreover, other method is to generate all the numbers together and then split them into list.

groups = 5
each = 5

setNames(split(rnorm(groups * each), rep(seq_len(groups), each = each)), 
         paste0("A", seq_len(groups)))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
1

I agree with the others that this is not a good idea. Anyway, to answer your question how you wolud do that is

k <- 1000 # number of variables
n <- 200 # sample size of each variable
for(i in 1:k){
assign(paste0("variable", i), rnorm(n, 0, 1))}
variable1
-0.012947062  0.728284959 -1.627796366  0.003471491 ...

However, personally I would prefer another solution. The both answers so far suggest using lists. I find lsts quite cumbersome, especially if you are new to R. So I would suggest creating a matrix where every column contains one variable.

# creates a matrix
m <- matrix(rep(NA, n*k), ncol= k)
# generates rnorm() in each column
for(i in 1:k){
m[ , i] <- rnorm(n, 0, 1)
}

# now you can name the columns
colnames(m) <- paste0("variable", 1:k)
m
        variable1   variable2    ...
 [1,]  0.30950749 -2.07388046
 [2,] -1.13232330 -0.55511476
  ...   
LulY
  • 976
  • 1
  • 9
  • 24
  • 1
    *"I find lsts quite cumbersome, especially if you are new to R."* You are going to have a tough time with R;-) A lot of objects in R are `list`s or `list`-like objects. A `list` is one of the *most fundamental* object in R. Understanding `list`s and how to work on/with them is critical to writing good R code. An R `list` is also not too dissimilar to a Python dictionary (but I'm sure Pythonians might disagree), see e.g. [this interesting post](https://stackoverflow.com/questions/50731319/python-equivalent-of-r-list). – Maurits Evers Apr 17 '19 at 05:49
  • @"You are going to have tough time": It took me some time to get used to lists and that is why I'd say they are tough for beginners. Anyway, I still prefer matrix and dataframes to lists because many functions can be applied easily, for example using colSums(m) or something like m * 2, where m is a matrix or a dataframe. – LulY Apr 17 '19 at 10:24
  • 1
    Here's the thing: A [`data.frame` *is* just a (special) `list`](https://stackoverflow.com/questions/15901224/what-is-difference-between-dataframe-and-list-in-r). So if anything then this shows that understanding `list`s is one of the most important concepts in R. They are no tougher or more cumbersome than variable types in other languages (it's the "cumbersome" descriptor that irks me most;-) In a way that's saying "coding in R" is the most cumbersome part of ... well ... coding in R. – Maurits Evers Apr 17 '19 at 10:31
  • I know what you mean. What I find not easy with lists is if they are nested multiple times or if the the elements inside the lists (dataframes, for examples) don't have the same dimensions which can lead to errors in functions. But I do agree that it is important to understand and use lists, of course :-) – LulY Apr 17 '19 at 10:41