For loop in R | Simulating Data using For loop
In different programming language and R, the for loop (for statement) allows one to specify the set of codes (commands) should be repeated a fixed number of times. For loop in R is not limited to integers or even number in the input. The character vectors, logical vectors, lists, or even expressions can also be used in for loop.
The general syntax of for loop is
for (named vector) { statements (R codes) }
The curly braces contains the set of commands so that these commands can be treated as single command and can be repeated desired number of times.However, if there is only single statement then there is no need to use curly races.
Let us understand the loop through different examples. Note that some of the examples can be done without the use of for loop or with alternatives such as apply()
, lapply()
, sapply()
, and tapply()
functions.
Example: Suppose you want to compute the squared values for 1 to 10. It can be done in for loop as;
for (i in 1:10){ squared <-i ^2 print(squared) }
Note: if you write print(squared) command outside the for loop (after the curly braces), then the last result of the loop iteration will be displayed in the console only, that is, the square of the last number (n = 10)
will be printed.
To store the result of each iteration in a variable (vector, matrix, data frame, or list), a container (variable) needs to be specified of the sample length as that of the loop have. For example, the outcome of each iteration (from the above example) can be stored in a variable as,
result <- vector("numeric", 10) for (i in length(result) ){ squared <- i^2 result[i] <- squared } result
Now results can be displayed without print()
command as the results are stored in a container (vector variable). To store results in a data frame or matrix (in the form of the table) with iteration number, the above example can be extended as
result <- data.frame(matrix(nrow = 10, ncol = 2)) colnames(result) <- c("i", "Square") for (i in 1:10 ){ squared <- i^2 result[i, 1] <- i # stores iteration number in 1stcolumn of data frame result[i, 2] <- squared # stores iteration result in 2nd column of data frame } result
Nesting For loop in R
Placing the loop inside the body of another loop is called nesting. For nested loops, the outer loop takes control of the iteration of the inner loop. The inner loop will be executed (iterated) n-times for every iteration of the outer loop. For example
for (i in 1:10){ for (j in 1:5){ print(i*j) } }
There will be a total of 50 iterations. For each iteration of the first loop (outer loop), there will be five iterations in the inner loop.
The break statement can be used inside a loop if one wants to stop the iteration when a certain condition (situation) occurs and the control will be out of the loop when the condition is satisfied. For example,
n <- 1:5 for (i in x){ if( i == 3){ break } print(i) }
It is also possible to jump to the next iteration using the next statement when certain condition satisfied. For example,
n <- 1:5 for( i in x ){ if (i==2){ next } print(i) }
Now consider the example of for loop using character vector
v = LETTERS[1:10] for(i in v){ print(i) }
Using For loop in Simulations
In simulations, to generate or resample (bootstrap) data, loops are used. For example, let create a variable having 1000 observations (n = 1000), where each observation is a function of the previous observation according to the equation
y<-rep(1,1000) for(i in 2:1000){ y[i]=0.8*y[i-1]+0.2*rnorm(1) } y
Consider another example of generating simulated data. Suppose, you want to simulate the mixture data and want to repeat it many times and you also want to store the data for each time.
n=100 res = list() N=1000 X = matrix(0, nrow=N, ncol=2) for(i in 1:n){ U = runif(N, min = 0, max = 1) for(j in 1:N){ if (U[j]<0.8){ X[j,] <- rnorm(1, 2.5, 3) } else{ X[j,] <- rnorm(1,2,1) } } res[[i]]=X } res[[100]]

Note that each res[[i]]
is a separate data set, which can be used for further calculations.