#Printing "Sankhya World" 4 times
print(replicate(4, "Sankhya World"))
[1] "Sankhya World" "Sankhya World" "Sankhya World" "Sankhya World"
Consider the amount of time that “for loops” use to run a function or expression a number of times, say 10,000 times. Understandably, it will take a lot of time, but the Replicate function offers a simpler and faster solution.
Replicate() makes it possible to produce repeated outcomes which can be used for generating test data sets, statistical analysis, simulations, and other uses.
replicate(n, expr)
Where,
n: the number of times to replicate an expression or function
expr: is a statement or function to evaluate
#Printing "Sankhya World" 4 times
print(replicate(4, "Sankhya World"))
[1] "Sankhya World" "Sankhya World" "Sankhya World" "Sankhya World"
Try it yourself: replace 4 by 500 and also, run the below code.
for (i in 1:500) { print(“Sankhya World”)}
Observe the time taken for the same task using “replicate()” and “for loop”. Even while it might only seem like a few milliseconds, we can only think how much time will be saved when dealing with massive volumes of data and thousands of loop runs.
<- c("Heads","Tails")
Toss<- replicate(25, sample(Toss, 1, replace = TRUE))
Toss_results Toss_results
[1] "Heads" "Tails" "Tails" "Heads" "Heads" "Tails" "Tails" "Heads" "Tails"
[10] "Tails" "Tails" "Heads" "Tails" "Heads" "Tails" "Heads" "Tails" "Heads"
[19] "Tails" "Heads" "Tails" "Heads" "Heads" "Heads" "Heads"
table(Toss_results)
Toss_results
Heads Tails
13 12
Here, sample(Toss, 1, replace = TRUE) randomly selects one value either “Heads” or “Tails”, simulating a single coin flip. replicate(25, …) repeats this simulation 25 times, giving you a vector of 25 Toss results.
Creating sample data sets which can be used as an input to model or testing algorithms
# Calculating mean and sd of 1 to 10 numbers
<-mean(1:10)
mean_val<-sd(1:10)
sd_val
# replicate normal distribution with mean and sd
<-replicate(4, rnorm(3, mean_val,sd_val))
rnormd_df rnormd_df
[,1] [,2] [,3] [,4]
[1,] 4.019644 3.8421126 7.331920 7.4974377
[2,] 2.169128 3.4926379 10.222299 0.1086222
[3,] 2.052114 0.6676298 9.312386 1.0337011
#Getting summary statistics for each sample(column)
summary(rnormd_df)
V1 V2 V3 V4
Min. :2.052 Min. :0.6676 Min. : 7.332 Min. :0.1086
1st Qu.:2.111 1st Qu.:2.0801 1st Qu.: 8.322 1st Qu.:0.5712
Median :2.169 Median :3.4926 Median : 9.312 Median :1.0337
Mean :2.747 Mean :2.6675 Mean : 8.956 Mean :2.8799
3rd Qu.:3.094 3rd Qu.:3.6674 3rd Qu.: 9.767 3rd Qu.:4.2656
Max. :4.020 Max. :3.8421 Max. :10.222 Max. :7.4974
Here, rnorm(3, mean_val,sd_val) creates our first sample with 3 set of values for the the mean and sd of 1 to 10 numbers. replicate(6,…) further repeats it for 4 times giving us a matrix of [3,4] i.e. 4 samples.
This time-saving function can greatly reduce the complexity of your repetitive coding jobs and thus optimize the code.