How to create a large vector with repetitive elements of varying size in R?

R ProgrammingServer Side ProgrammingProgramming

To create a large vector of repetitive elements of varying size we can use the rep function along with the logical vector as an index. The logical vector that contains TRUE or FALSE will define the selection or omission of the values in the vector created with the help of rep function as shown in the below examples. If the vector created by using rep is larger than the logical vector then the logical vector will be recycled.

Example1

 Live Demo

x1<−rep(1:100,each=2)[c(TRUE,FALSE)]
x1

Example

 Live Demo

x1<−rep(1:100,each=2)[c(TRUE,TRUE)]
x1

Output

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

Example

 Live Demo

x1<−rep(1:100,each=2)[c(TRUE,TRUE,FALSE)]
x1

Output

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

Example2

 Live Demo

x2<−rep(0:1,each=100)[c(TRUE,TRUE,FALSE,TRUE,FALSE)]
x2

Output

[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[38] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[75] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[112] 1 1 1 1 1 1 1 1 1

Example3

 Live Demo

x3<−rep(1:50,each=5)[c(TRUE,FALSE,TRUE,TRUE,TRUE)]
x3

Output

[1] 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5 6 6 6 6 7
[26] 7 7 7 8 8 8 8 9 9 9 9 10 10 10 10 11 11 11 11 12 12 12 12 13 13
[51] 13 13 14 14 14 14 15 15 15 15 16 16 16 16 17 17 17 17 18 18 18 18 19 19 19
[76] 19 20 20 20 20 21 21 21 21 22 22 22 22 23 23 23 23 24 24 24 24 25 25 25 25
[101] 26 26 26 26 27 27 27 27 28 28 28 28 29 29 29 29 30 30 30 30 31 31 31 31 32
[126] 32 32 32 33 33 33 33 34 34 34 34 35 35 35 35 36 36 36 36 37 37 37 37 38 38
[151] 38 38 39 39 39 39 40 40 40 40 41 41 41 41 42 42 42 42 43 43 43 43 44 44 44
[176] 44 45 45 45 45 46 46 46 46 47 47 47 47 48 48 48 48 49 49 49 49 50 50 50 50

Example4

 Live Demo

x4<−rep(1:100,each=5)[c(TRUE,FALSE,TRUE)]
x4

Output

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