How to create the random sample by defining the probabilities for each unit in R?

R ProgrammingServer Side ProgrammingProgramming

The random sample can be created by using sample function, this random sample gives equal chance for each unit to be selected in the sample, hence it is called simple random sample. If we want to have a sample where each unit has different chance of being selected in the sample then we need to use the argument prob as shown in the below examples.

Example1

 Live Demo

x1<−sample(c(1,2,3,4,5),150,replace=TRUE,prob=c(0.1,0.2,0.2,0.2,0.3))
x1

Output

[1] 3 5 1 5 5 2 4 3 5 2 4 1 4 2 3 4 4 5 2 5 1 1 5 5 3 5 4 2 1 1 5 3 5 4 2 5 2
[38] 5 4 2 3 4 5 3 5 1 4 5 2 4 1 4 5 3 3 5 5 4 5 3 5 4 5 4 3 4 5 3 2 3 3 4 3 2
[75] 3 4 5 4 2 5 2 4 4 4 4 5 3 4 5 5 2 4 2 2 2 4 4 2 5 3 2 5 1 2 3 5 1 3 1 2 3
[112] 3 2 4 5 4 1 3 5 4 4 3 5 1 1 5 2 2 2 4 3 4 4 5 4 5 5 5 5 3 2 3 5 5 5 2 4 3
[149] 5 5

Example

table(x1)

Output

x1
1 2 3 4 5
14 27 28 36 45

Example2

 Live Demo

x2<−sample(1:5,150,replace=TRUE,prob=c(0.1,0.2,0.2,0.2,0.3))
x2

Output

[1] 5 2 5 2 3 4 3 4 5 3 3 2 5 2 5 4 4 3 5 2 1 5 5 5 2 5 2 5 5 5 2 4 3 4 4 4 3
[38] 2 5 5 5 2 2 4 4 5 2 2 3 5 5 5 4 3 4 5 5 3 3 3 3 2 3 2 4 5 3 3 2 5 1 5 5 2
[75] 2 3 3 4 3 5 4 5 5 5 4 4 5 4 3 3 3 2 2 4 5 3 5 5 5 5 2 2 5 3 3 5 2 3 4 5 4
[112] 4 5 4 3 2 3 2 4 5 1 2 1 3 5 2 4 4 3 3 4 1 3 1 5 5 5 4 1 1 2 1 5 3 3 1 5 1
[149] 4 5

Example

table(x2)

Output

x2
1 2 3 4 5
11 28 34 29 48

Example3

 Live Demo

x3<−sample(0:9,200,replace=TRUE,prob=c(rep(1/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 0 8 8 6 1 1 6 7 4 3 1 1 0 9 4 5 8 1 0 2 6 5 6 8 4 0 8 0 0 6 7 7 4
[260] 2 9 6 2 7 6 2 5 3 0 7 8 3 1 6 7 0 5 5 2 6 8 6 8 2 5 7 7 4 7 3 7 2 1 1 7 4
[297] 3 7 9 9 9 6 5 4 4 2 0 5 1 4 2 7 4 7 5 8 0 7 6 7 1 6 5 3 5 6 4 3 3 2 4 7 5
[334] 1 8 3 1 6 1 8 5 5 0 9 7 6 0 6 2 7 1 2 4 9 8 3 0 7 8 2 9 6 2 3 4 1 1 6 5 6
[371] 1 6 1 0 7 5 1 3 8 6 9 2 2 1 7 2 3 1 5 4 7 2 0 7 9 5 0 8 6 9

Example

table(x4)

Output

x4
0 1 2 3 4 5 6 7 8 9
41 40 43 38 33 45 40 55 41 24

Example5

 Live Demo

x5<−sample(1:100,200,replace=TRUE,prob=c(rep(1/100,100)))
x5

Output

[1] 55 42 48 27 55 1 69 41 83 20 65 83 55 24 30 52 38 98
[19] 82 9 3 27 58 100 3 90 60 79 71 96 4 42 35 73 31 29
[37] 94 13 92 15 35 1 61 73 14 46 98 69 72 5 41 54 81 9
[55] 74 86 72 88 54 37 55 9 7 3 9 69 100 65 73 85 64 33
[73] 50 76 15 19 13 57 42 9 1 58 78 95 6 43 35 48 8 69
[91] 44 4 82 52 1 37 88 74 50 52 25 27 28 74 83 52 60 87
[109] 84 97 62 15 98 30 92 44 76 33 72 62 98 37 59 40 16 44
[127] 26 67 46 91 23 31 100 80 18 42 51 45 44 85 97 3 87 65
[145] 62 51 36 51 18 55 20 15 53 84 6 61 40 23 24 67 28 55
[163] 86 52 13 28 36 22 50 67 79 33 31 43 86 43 73 26 89 29
[181] 52 36 6 12 40 44 4 65 92 16 33 86 76 21 62 64 94 25
[199] 52 33

Example

table(x5)

Output

x5
1 3 4 5 6 7 8 9 12 13 14 15 16 18 19 20 21 22 23 24
4 4 3 1 3 1 1 5 1 3 1 4 2 2 1 2 1 1 2 2
25 26 27 28 29 30 31 33 35 36 37 38 40 41 42 43 44 45 46 48
2 2 3 3 2 2 3 5 3 3 3 1 3 2 4 3 5 1 2 2
50 51 52 53 54 55 57 58 59 60 61 62 64 65 67 69 71 72 73 74
3 3 7 1 2 6 1 2 1 2 2 4 2 4 3 4 1 3 4 3
76 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 94 95 96 97
3 1 2 1 1 2 3 2 2 4 2 2 1 1 1 3 2 1 1 2
98 100
4 3
raja
Published on 10-Feb-2021 07:16:07
Advertisements