How to select random elements from an R vector?

R ProgrammingServer Side ProgrammingProgramming

Random selection of elements from a R vector ensures the unbiased selection because while doing the random selection, each of the elements of the vector gets an equal probability of being selected by the random selection procedure specifically the simple random sampling selection procedure. To select, one or more elements randomly from an R vector, we can use sample function.

Example

> x1<-1:50
> x1
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
[26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
> sample(x1,1)
[1] 42
> sample(x1,5)
[1] 6 24 32 14 2
> sample(x1,10)
[1] 45 18 22 14 38 1 40 6 23 43
> sample(x1,10,replace=TRUE)
[1] 40 39 11 17 36 13 40 25 48 25
> sample(x1,100,replace=TRUE)
 [1] 23 46 20 39 29 13 22 29 28 44 48 33 45 21 31 38 17  9 39 23 19 50 26 48 43
[26] 30 42 32 39 29 34 10  1 43 26 15 29 24 42 48 12 39 24 28 22 40 33 19 26 35
[51] 43  1 29 14 22  6 28 41 37 46 49  3 22 35  7 46 35 44 49 41 50 49 50 47 27
[76]  7 47 20 24 16 19 36  2 48  1 44 39 41 13 26 34  2 19 17 34 11 31 35 35 46
> sample(x1,500,replace=TRUE)
  [1] 19 45 11 16 40  9 50 28 24 10 15 37 32 37 39 31 37 29 12 14 38 16 15  2  1
 [26]  3  9 38 17 20  5 41 27 44 31  8 36 49 41 29 21  1 20 42 46 42 24 22 38 50
 [51] 16 30 29  2  8 14 36 26 40 37  8  7 36 49 42 36  9 27 41 15 38  6  4 35 35
 [76] 13 41  7 25 31 46 37 28 39 28 20 29 42 40  1 28 14 31 12 29 16 44 10 34 26
[101] 49 33  4 22 25 24 37 44 15 14 47 17 47 18 33 32  8 42 39  7 27 43 50  9 20
[126] 34 37 29 11  2  3 47 38 41 15 28  5 28 14 37 43 43 17  1 26 45 33 19 26 23
[151]  4 16 49 27 39 38 16 34 29 45 35 10  6 39 49 33 23 33 35 11 13 50 37 26 26
[176] 48 40 34  4 25 38 17 24  9 45 46 30  9 11 22 32 40  3 19 33  4 13  1 40  5
[201] 42 44 35 23 29 35 36  9 35 35 20 22 41 22 19 44 18 44 39 29 27 36 32 31 17
[226] 37 25  9 21 21 15 37 24 14  7 27 26 30 45 15 50  6 34 21  1 17 13 45 26 40
[251]  2 41  8 25 31 20 46 20  5 10 44 20 15 29  2  1  5 25 37 32 40 13 25  4 42
[276] 38 50  5  1 38 46 13  8 49 19 48 47 28 48 49 46 38 11  8 23 20  5  2  5 12
[301]  3 26 50 14 13 31 47 34 30 26 45 48  7 48 27 49 43  9  8 22 45  6 48 27 44
[326] 30 10 30  1  6 17 13 26  2 32 47 36 44  5 23 38 20 28 28 12 35  4 47 46 26
[351] 13  3 46 15 34 38 48  4 26 21 21 46 16 37 24 21  3 26 23 36 49 22 47  3 48
[376] 48  4 36 37  1 47 37 22 21 27 11 44 11  6 48 36  6 20 40 33 40 11 11 21 21
[401] 17  9 18 31 44  8 23  6 11 37 25  2 39 32 42 40 22 35 11 13  2 40  6 15 41
[426] 28  1  2  9 49  5 27 42 29  2 37 31 14 25 21 23  6 48 21  5 25 17 33 40 19
[451] 17 20 23 20 48 49 31  5 27 33  1 29 34 36 20 21 38 24 34 47 20 50  3 20 34
[476] 15 30 42 13 16 43 36 48 30 47 29 28 13 48  7 18  1  4  5 46 36 34 26 33 19
> sample(x1,500,replace=FALSE)
Error in sample.int(length(x), size, replace, prob) :
cannot take a sample larger than the population when 'replace = FALSE'

Here, we are getting the error because the size of vector x1 is not greater than the sample size 500. If we want to create a large vector than the available vector then we have to put the condition “replace = TRUE”. Or we can create the larger vector from a vector that has large size than the original one as shown below −

> x2<-1:5000
> sample(x2,200,replace=FALSE)
[1]  1716 3437 582 576 1817 3114 225 2956 3060 185 2841 2426 4609 4083 245
[16] 1057 3679 2888 3842 4711 2928 450 665 1670 614 3313 1090 1249 3715 1798
[31] 4836 4589 4596 2403 39 3833 1552 3176 721 4754 1961 1432 386 2955 4910
[46] 4460 4896 1044 2843 327 4039 3800 1311 3330 837 1205 3565 3237 1768 4038
[61] 2296 435 1693 3316 1852 838 3212 2490 4474 4703 4649 4630 3977 3878 556
[76] 3322 3003 4346 306 2435 945 2772 4826 1279 749 4286 224 2507 4220 3448
[91] 2095 3268 219 4904 626 4360 2765 2240 4588 4105 3721 534 3022 2423 1838
[106] 1526 2389 401 3279 4780 3929 34 3728 116 2046 3288 2751 877 2374 347
[121] 3984 1302 3755 4178 571 4866 855 3580 4688 4385 2672 1882 2206 68 4618
[136] 823 173 391 2350 505 4475 3983 2289 3444 3553 935 1316 3327 4894 3936
[151] 2878 2408 2438 90 2836 1623 1514 4473 2600 2499 1689 3510 3733 1954 3387
[166] 4142 4344 1511 773 3524 3375 3650 2145 40 41 4239 113 3890 4651 3152
[181] 2255 3158 133 2690 4211 1641 4527 794 4111 4387 4612 1813 3159 2705 1830
[196] 1359 1581 2904 4778 2579
raja
Published on 11-Aug-2020 14:25:26
Advertisements