# How to generate a repeated values vector with each value in output selected randomly in R?

To generate a repeated values vector with each value in output selected randomly in R, we can use replicate function. The replicate function will repeat the vector values up to the number of times we want and each value will be randomly selected. To understand how it works, check out the below examples.

## Example 1

Using replicate function to create a vector of exponential random variable of size 10 −

x1<-replicate(10,rexp(1))
x1

## Output

[1] 2.32622226 1.43769104 0.84140379 0.31309834 0.34728869 1.12669808
[7] 2.59806767 0.95124064 0.65973341 0.02458311

## Example 2

Using replicate function to create a vector of exponential random variable of size 100 −

x2<-replicate(100,rexp(1))
x2

## Output

[1]  1.338218972 0.928111715 0.210226591 0.782096210 0.980464659 0.561102964
[7]  0.420678409 1.521477580 2.657032652 0.210767035 1.083586779 1.428215502
[13] 0.495238871 0.117252999 0.065553573 2.023542229 0.139094335 0.284678072
[19] 1.876679134 0.107705118 0.333504217 0.839815633 0.051048229 0.326633231
[25] 1.073403904 2.515174685 1.925430402 0.465222352 4.646641928 0.909072626
[31] 6.086874112 0.409404304 0.218934411 0.442255062 0.199396318 1.125906858
[37] 0.094596248 1.504960904 1.905449165 0.983241949 2.725240568 3.174215562
[43] 0.347061660 1.387700094 0.939728857 0.428349982 2.265757175 0.965756987
[49] 0.183605692 0.283113697 0.565889680 0.655143598 1.362383223 0.059399699
[55] 2.679196225 0.198336834 0.083796074 0.856362864 0.515979461 1.122302708
[61] 1.335402171 2.133741173 1.026671943 0.976029757 3.499395951 0.442655258
[67] 2.525529204 0.255688451 0.317069784 1.036617768 0.267435749 1.819084953
[73] 0.279127992 4.715847362 1.673101706 3.370964892 0.328154675 1.485599963
[79] 0.096166832 0.267245952 1.490545847 0.796993139 0.788134819 0.256215425
[85] 1.545049881 0.499626923 0.004774265 1.833408965 2.896811513 0.552053340
[91] 1.786220800 1.207105992 4.116648873 0.355176195 1.823150722 0.830110544
[97] 0.405113572 0.029689468 2.242159443 1.004736981

## Example 3

Using replicate function to create a vector of normal random variable of size 100 −

x3<-replicate(100,rnorm(1))
x3

## Output

[1]   0.153288421 -1.219525465 -0.625330755 -0.456268249 -0.045359052
[6]  -0.902709458 -0.716718796 -0.214672818 -0.600083002 -1.960343132
[11] -1.677517129  1.064098446  0.385287036 -0.091449498  0.394187375
[16]  0.882923958 -1.823264675  0.330432747  1.762251038  1.327119063
[21] -0.949451629  0.149645508  1.868005006  0.770408718  1.272448989
[26] -0.168494279 -0.107068400  1.472309649 -0.150467262 -0.152496721
[31]  0.769591215  1.153377443  0.477914235  1.596813635 -0.440377190
[36]  0.061882121  0.106553833  1.495253813  1.012869670 -1.018312385
[41]  1.198279836  0.124804984 -0.374792946  1.762658378 -0.312889224
[46]  0.399795624 -0.475596079  0.894190442 -0.795381419  0.436562146
[51] -0.357577709  0.700715436  0.402455839 -0.007842341 -0.589435217
[56]  0.777849457 -1.580414451 -0.035547423 -1.085388646 -1.979881190
[61]  0.392376294  0.281403338 -1.396385118  1.932603607 -0.014650476
[66]  0.599278722 -0.281731560  0.381315576 -1.016807561  0.805428495
[71]  0.516286173 -0.722918222 -1.027853561 -0.317724841 -0.378129174
[76]  0.252757886 -0.879654650 -1.537333772 -0.307502629  1.717763788
[81] -0.228402374 -2.025650718 -1.381842544 -0.106518292 -1.355510949
[86]  1.240272007  0.390788562  0.038186514 -1.041538991 -2.971225093
[91]  1.105531036 -1.445929659 -1.136905186 -0.128538626  1.101869153
[96] -0.099773963  0.929702356  2.176688511  0.838853049  1.999621713

## Example 4

Using replicate function to create a vector of poisson random variable of size 250 −

x4<-replicate(250,rpois(1,25))
x4

## Output

 [1]  20 24 23 33 27 13 29 29 29 29 24 24 31 32 23 28 27 18 33 24 21 29 26 21 32
[26]  26 28 37 21 18 22 36 26 25 20 23 22 28 31 38 22 20 25 25 33 27 17 33 23 34
[51]  34 26 23 15 23 22 35 30 23 25 27 32 27 24 20 30 24 27 22 26 27 26 22 27 31
[76]  24 27 30 42 29 16 25 32 21 20 34 30 33 37 30 24 29 34 29 25 22 33 29 28 28
[101] 27 13 32 24 26 35 25 28 27 20 26 29 23 22 25 24 20 26 28 19 35 27 25 23 30
[126] 29 31 24 24 23 23 23 20 22 24 18 21 34 22 14 33 21 19 11 17 33 19 26 30 26
[151] 23 26 25 33 28 18 27 31 23 24 19 35 21 29 18 30 20 26 32 16 15 25 26 32 24
[176] 31 22 18 25 22 28 22 21 21 32 31 25 20 33 19 27 25 24 34 23 21 28 18 28 22
[201] 23 21 28 33 20 28 33 21 22 32 26 20 17 21 25 27 25 25 20 34 16 17 22 18 30
[226] 25 30 33 22 29 23 34 25 25 17 31 24 16 28 27 19 25 29 24 30 27 19 23 26 21