How to create a random vector for a range of values in R?


To create a random vector for a range of values, we can use sample function. We just need to pass the range and the sample size inside the sample function. For example, if we want to create a random sample of size 20 for a range of values between 1 to 100 then we can use the command sample(1:100,20) and if the sample size is larger than 100 then we can add replace=TRUE as shown in the below examples.

Example

 Live Demo

x1<-sample(101:255,20)
x1

Output

[1] 242 213 223 132 138 232 251 229 151 170 136 206 153 152 116 173 162 169 181
[20] 225

Example

 Live Demo

x2<-sample(101:255,200,replace=TRUE)
x2

Output

[1] 243 124 176 166 220 196 171 189 132 174 213 118 102 153 208 108 183 191
[19] 150 143 113 175 172 115 180 101 176 101 195 137 167 252 208 145 115 255
[37] 144 147 187 212 158 227 112 254 196 195 169 239 233 198 210 106 107 219
[55] 198 249 220 216 136 156 146 235 160 243 219 102 157 241 131 151 236 243
[73] 180 141 103 201 135 154 192 108 133 214 118 119 112 160 117 141 107 103
[91] 122 137 217 181 246 187 192 202 143 119 217 153 220 150 214 246 189 128
[109] 192 220 217 239 215 220 201 223 240 246 177 246 231 117 115 252 118 144
[127] 234 235 225 156 157 228 154 186 116 250 158 180 238 244 208 191 168 254
[145] 214 207 193 118 221 193 157 192 178 190 235 245 241 128 111 118 108 232
[163] 223 234 218 158 169 108 152 233 181 209 145 186 138 140 215 164 206 142
[181] 118 139 115 220 171 158 242 183 149 137 141 175 102 145 247 189 124 111
[199] 254 123

Example

 Live Demo

x3<-sample(1001:9999,200)
x3

Output

[1] 2653 3824 4378 6082 7773 6379 6820 6971 9129 5263 8355 7122 6405 4663 3455
[16] 4727 9448 2020 7312 4542 8563 3743 8069 3704 5256 3654 6245 7268 8017 4236
[31] 2228 8370 1466 6734 5531 7194 1691 5464 8803 4535 4521 3505 1347 8082 2422
[46] 8269 4613 4040 5816 3706 9936 7317 2448 5246 1345 7606 6997 1197 1387 6561
[61] 7957 5622 2258 5486 6529 4576 2149 6499 3506 4584 4014 5433 9177 2763 5129
[76] 9328 8586 7030 8263 2888 4826 3181 1844 7039 8313 7668 1982 3445 2435 2597
[91] 8866 5095 3855 4037 9605 7739 8861 6639 9483 8133 3359 3144 7311 2679 8840
[106] 4188 4972 8137 9517 6628 3977 9032 4926 8238 2782 9886 6973 8419 6630 1081
[121] 5303 2692 3022 1138 7048 6908 3830 5290 3447 9170 5814 7488 1326 6380 4452
[136] 9375 5252 9519 6436 3623 7811 2067 7285 8851 5140 9206 5781 2624 6564 9299
[151] 7686 2095 3546 7906 7570 9460 5385 3620 6027 5022 6144 5736 3167 9323 8954
[166] 6127 6620 9187 9861 1802 4920 9910 5534 2659 6387 5318 8736 6927 4784 1024
[181] 1204 3914 1093 7340 9005 6036 1530 8020 4084 8343 7319 6172 1004 2399 3836
[196] 8514 6819 9951 8906 5163

Example

 Live Demo

x4<-sample(111111:999999,200)
x4

Output

[1] 600312 407855 966964 875349 904506 926894 238005 502611 870129 942176
[11] 830987 274868 837025 456457 991641 667857 282525 691272 952168 587564
[21] 299470 653862 913685 523621 705830 605779 254650 692032 760329 595360
[31] 859846 983655 476633 469309 703161 896174 694116 843369 694332 893344
[41] 998538 803770 405526 862820 700027 498387 520287 601006 408811 731184
[51] 510417 315797 600711 856155 925074 231724 145128 588790 705880 489333
[61] 870712 447203 628393 940431 852196 745619 129424 587357 401113 123174
[71] 930736 538683 577927 767703 803663 724039 184337 488391 485942 767882
[81] 396737 662066 536730 628748 314601 163375 924908 404496 962551 538693
[91] 658156 760170 555808 464672 739164 992975 305519 929500 962073 568831
[101] 868815 749854 486654 287465 225997 528227 554214 931047 888718 505141
[111] 840700 507121 544464 124253 346562 917076 531122 588485 814361 514039
[121] 420468 856792 238455 994923 185076 605198 633359 691691 958025 284669
[131] 554292 127470 936445 363940 845046 763377 493902 217931 252551 398839
[141] 151523 766029 891409 463939 405146 464720 587105 145960 545138 315821
[151] 801428 436091 651978 377162 689570 278554 164927 510315 317276 491635
[161] 998260 238756 327236 190089 688942 865359 771590 970798 200009 541251
[171] 735520 615970 985052 918840 394681 831838 950972 521854 640459 829664
[181] 553789 259684 387660 415205 633093 265813 649319 518413 564418 902089
[191] 301216 315056 307910 336221 981584 533996 259812 421166 903476 489474

Updated on: 16-Mar-2021

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements