How to create random values in R up to a range of values starting from 1?

R ProgrammingServer Side ProgrammingProgramming

Random sampling is a technique used by almost every researcher, analyst, financial analyst, data scientist, or even a leader and if we way that almost everyone uses it at least once in a lifetime then it won’t be surprise. Because we use it in one or the way in our life even if we don’t know about it. To take a random sample or creating random values up to a range of values starting from 1, we can simply use sample function in R. Checkout below examples to understand how this function works for sampling with replacement.

Example

 Live Demo

sample(100)

Output

[1] 17 76 16 99 37 77 19 54 51 11 30 50 73 4 66 96 25 43
[19] 38 58 40 3 88 14 41 48 5 26 61 1 42 59 31 34 74 53
[37] 2 57 64 83 46 33 85 12 23 72 27 56 49 62 69 20 28 84
[55] 6 98 10 29 75 55 89 7 24 52 97 22 35 47 36 91 21 92
[73] 67 65 63 93 95 60 68 44 82 86 78 79 45 80 39 70 71 9
[91] 8 13 15 94 32 100 81 87 18 90

Example

 Live Demo

sample(100,50)

Output

[1] 65 64 47 18 95 44 61 48 10 4 71 23 74 36 92 75 90 13 76
[20] 39 93 81 20 66 6 100 11 17 27 62 55 78 37 14 91 52 35 96
[39] 12 1 87 67 70 22 88 38 94 83 63 82

Example

 Live Demo

sample(1000,50)

Output

[1] 419 47 992 275 204 263 316 394 595 93 252 256 109 359 297 288 367 185 805
[20] 691 709 854 954 883 249 797 614 906 205 409 267 893 747 832 56 637 909 659
[39] 304 859 125 974 658 353 581 381 696 717 134 824

Example

ample(1000,91)

Output

[1] 648 251 146 495 783 805 909 869 965 305 636 166 86 393 27 75 913 317 253
[20] 919 997 879 410 227 243 839 341 191 382 821 917 730 157 979 138 348 711 18
[39] 630 320 668 707 615 288 128 850 453 45 853 765 558 59 347 955 597 664 245
[58] 123 241 460 763 951 514 470 156 248 521 611 10 643 996 101 725 216 414 594
[77] 494 487 110 924 187 578 489 506 247 722 454 885 74 158 448

Example

 Live Demo

sample(500,100)

Output

[1] 332 493 196 291 287 7 81 217 315 403 132 112 242 356 462 333 148 344
[19] 285 220 371 89 191 255 410 2 193 117 379 123 323 413 444 489 478 256
[37] 16 408 51 116 134 49 331 185 348 374 370 55 464 125 486 394 238 475
[55] 209 463 54 15 82 149 299 144 395 341 330 73 225 316 325 337 396 283
[73] 45 450 336 71 87 385 388 229 252 279 245 387 84 293 121 173 244 72
[91] 107 186 280 290 467 6 358 496 44 300

Example

 Live Demo

sample(5000,100)

Output

[1] 2866 1649 39 2748 708 4479 986 805 3387 396 4659 4449 4590 2882 3973
[16] 3232 1366 4706 1552 982 2511 3214 4687 2786 1275 2038 770 4078 4907 2016
[31] 4867 2859 2873 3278 4527 119 3132 1451 4794 129 4184 4144 846 1299 4887
[46] 2642 2555 2895 753 4006 4284 4159 839 2201 3043 3351 3674 767 666 3466
[61] 1911 499 408 132 1047 1188 3083 3217 672 1488 280 4829 3834 230 696
[76] 917 2829 3791 1704 3166 4012 116 1599 2912 1547 1727 1168 1697 3051 4482
[91] 2665 4038 4496 1134 4036 773 857 364 3708 2054

Example

 Live Demo

sample(999,100)

Output

[1] 740 22 791 914 868 625 925 253 301 566 449 80 188 505 300 862 472 57
[19] 681 127 512 400 874 617 613 813 83 527 150 823 506 58 755 790 441 561
[37] 289 243 960 633 246 393 182 511 504 968 601 831 79 352 215 296 514 461
[55] 747 288 297 540 879 864 702 860 756 126 638 214 562 901 308 435 318 777
[73] 761 87 779 557 313 835 412 341 391 923 727 692 608 446 707 673 193 586
[91] 771 818 829 811 229 100 206 343 699 32

Example

 Live Demo

sample(10000,100)

Output

[1] 2685 9775 2365 8625 1584 6600 3132 4447 3618 3899 3824 9850 8016 418 4251
[16] 3358 7802 5586 5448 2712 8007 8059 4914 2719 5234 7983 6548 965 2732 1513
[31] 3931 4792 4348 5689 6151 5285 3148 9331 2508 1279 512 7505 917 9385 2205
[46] 2418 8179 824 4036 2134 1899 3582 1999 1335 7047 5493 7416 6519 2938 1287
[61] 9859 1285 6229 7143 8259 3997 9724 5348 4363 9507 8514 2776 2584 384 4992
[76] 8424 5898 1923 459 5724 6288 83 9160 5778 6307 515 8309 4358 8835 7480
[91] 1356 552 6290 7236 316 5158 139 4123 111 5698

Example

 Live Demo

sample(10000000,100)

Output

[1] 9983192 2980583 6700110 6541794 4143640 3781046 8327250 1193097 1958518
[10] 7627508 8024759 907803 4875744 4256714 5756553 6689123 539355 1727612
[19] 2785020 59529 7792184 3310830 2555942 5243004 7431258 8123459 3368725
[28] 6553789 4949766 1568820 4609213 6752782 3970705 9786129 9938046 5756106
[37] 4827050 8247485 2731518 427370 6894243 8460883 1477596 6688585 592789
[46] 7075440 5831707 5564820 5249755 7496462 4824093 2805153 2236860 4450245
[55] 8421355 3738343 5298959 9003315 5490426 7392566 1143293 5309013 942898
[64] 8614787 773804 2700281 4982792 540333 589711 3432082 645514 6041038
[73] 7619427 6843043 9421728 6054981 7326272 6645130 8714433 4755913 5361624
[82] 9358636 4102569 4265342 7590595 8568894 3993504 9162176 1857800 9161009
[91] 1835886 6580707 1435928 8381474 4104416 2467922 2103598 7611009 6708870
[100] 1800486

Example

 Live Demo

sample(12345,100)

Output

[1] 510 764 10055 1085 8422 6867 5385 1835 8668 1623 10870 8090
[13] 11345 501 8346 12301 9539 2507 4540 889 2107 2045 11795 4668
[25] 9325 138 11141 9012 5652 5772 4186 1362 36 7420 6150 504
[37] 4860 4137 1044 5554 4973 10379 11505 8249 11716 7921 9918 11913
[49] 8853 3129 8323 8148 1301 9468 10291 1759 8728 7336 5844 2877
[61] 5920 3880 3845 1431 517 6200 9776 3264 11790 10288 6669 2421
[73] 5805 11754 10994 2982 3775 2555 6479 1488 2228 5432 49 1744
[85] 1074 7480 4250 5862 4960 2993 7246 8307 10095 2795 11756 6049
[97] 11238 5383 5371 1745

Example

 Live Demo

sample(54321,100)

Output

[1] 1800 8556 49538 32167 23228 40749 5334 19909 40409 6244 41947 47461
[13] 44971 6386 19601 35310 32304 29637 4018 23563 49141 36949 3769 50944
[25] 18570 24743 8582 49820 10898 6485 5885 37039 16767 5307 15104 12769
[37] 37061 16981 38944 44214 22631 20207 28114 15134 41289 17073 13851 49327
[49] 32892 19421 4133 14683 31502 36138 14649 12336 38636 20705 6697 4940
[61] 27218 43723 39446 15831 35616 44896 23150 47271 3857 28333 17920 32138
[73] 25837 47030 8601 36998 25452 35042 22103 32078 5689 47914 24561 16642
[85] 7164 32331 38350 8517 34347 37764 4681 35343 46978 17529 45771 9672
[97] 50343 33303 40246 1057
raja
Published on 16-Oct-2020 17:25:20
Advertisements