How to concatenate elements of equal size vectors alternatively in R?

R ProgrammingServer Side ProgrammingProgramming

If we have multiple vectors and we want to combine the vector elements alternatively then we can use rbind function along with c function.

For Example, if we have three vectors say X, Y, and Z as −

X = 1, 2, 3
Y = 4, 5, 6
Z = 7, 8, 9

Then, we can combine the elements in these vectors alternatively by using the below command −

c(rbind(X,Y,Z,))

Example 1

To concatenate elements of equal size vectors add the following code to the above snippet −

x1<-rpois(50,5)
x1

Output

If you execute all the above given snippets as a single program, it generates the following Output −

[1] 5 6 1 5 6 7 2 7 6 4 9 10 3 5 8 7 4 3 5 5 8 5 4 3 6
[26] 1 5 5 8 6 5 4 2 7 2 3 6 4 7 8 3 5 2 6 2 6 6 0 8 7

To concatenate elements of equal size vectors add the following code to the above snippet −

x1<-rpois(50,5)
x2<-rpois(50,5)
x2

Output

If you execute all the above given snippets as a single program, it generates the following Output −

[1] 7 2 3 2 3 3 3 7 4 6 5 7 3 5 3 5 5 7 6 3 4 6 5 3 2
[26] 5 9 6 5 5 1 0 4 10 5 6 4 4 7 4 5 9 1 3 5 8 8 2 6 4

To concatenate elements of equal size vectors add the following code to the above snippet −

x1<-rpois(50,5)
x2<-rpois(50,5)
x3<-rpois(50,5)
x3

Output

If you execute all the above given snippets as a single program, it generates the following Output −

[1] 7 6 3 6 3 4 3 2 6 4 1 6 2 4 5 5 9 7 6 10 2 4 4 4 8
[26] 9 3 4 3 6 10 6 3 7 4 4 3 4 3 6 8 9 6 4 7 6 6 4 5 6

To concatenate elements of equal size vectors add the following code to the above snippet −

x1<-rpois(50,5)
x2<-rpois(50,5)
x3<-rpois(50,5)
x4<-rpois(50,5)
x4

Output

If you execute all the above given snippets as a single program, it generates the following Output −

[1] 3 4 6 5 8 4 5 2 6 5 3 5 2 10 3 5 4 9 4 3 7 6 12 5 2
[26] 10 5 11 9 5 7 6 3 2 8 7 5 2 3 4 5 4 8 2 7 4 12 2 5 6

To concatenate elements of equal size vectors add the following code to the above snippet −

x1<-rpois(50,5)
x2<-rpois(50,5)
x3<-rpois(50,5)
x4<-rpois(50,5)
c(rbind(x1,x2,x3,x4))

Output

If you execute all the above given snippets as a single program, it generates the following Output −

[1] 5 7 7 3 6 2 6 4 1 3 3 6 5 2 6 5 6 3 3 8 7 3 4 4 2
[26] 3 3 5 7 7 2 2 6 4 6 6 4 6 4 5 9 5 1 3 10 7 6 5 3 3
[51] 2 2 5 5 4 10 8 3 5 3 7 5 5 5 4 5 9 4 3 7 7 9 5 6 6
[76] 4 5 3 10 3 8 4 2 7 5 6 4 6 4 5 4 12 3 3 4 5 6 2 8 2
[101] 1 5 9 10 5 9 3 5 5 6 4 11 8 5 3 9 6 5 6 5 5 1 10 7 4
[126] 0 6 6 2 4 3 3 7 10 7 2 2 5 4 8 3 6 4 7 6 4 3 5 4 4
[151] 4 2 7 7 3 3 8 4 6 4 3 5 8 5 5 9 9 4 2 1 6 8 6 3 4
[176] 2 2 5 7 7 6 8 6 4 6 8 6 12 0 2 4 2 8 6 5 5 7 4 6 6

Example 2

To concatenate elements of equal size vectors add the following code to the above snippet −

y1<-sample(LETTERS[1:26],50,replace=TRUE)
y1

Output

If you execute all the above given snippets as a single program, it generates the following Output −

[1] "V" "J" "M" "O" "H" "N" "D" "K" "H" "D" "B" "V" "I" "N" "C" "U" "U" "E" "Q"
[20] "N" "H" "G" "U" "I" "B" "Z" "L" "Q" "C" "M" "W" "E" "Q" "V" "T" "D" "V" "O"
[39] "U" "P" "W" "R" "X" "H" "L" "E" "I" "X" "B" "B"

To concatenate elements of equal size vectors add the following code to the above snippet −

y1<-sample(LETTERS[1:26],50,replace=TRUE)
y2<-sample(LETTERS[1:26],50,replace=TRUE)
y2

Output

If you execute all the above given snippets as a single program, it generates the following Output −

[1] "J" "F" "L" "T" "Z" "I" "C" "M" "X" "W" "G" "U" "Q" "X" "S" "I" "E" "F"
"B"
[20] "G" "B" "N" "D" "R" "W" "V" "K" "F" "H" "R" "V" "J" "K" "V" "J" "J" "O"
"F"
[39] "T" "V" "Y" "X" "W" "B" "S" "J" "M" "D" "Y" "V"

To concatenate elements of equal size vectors add the following code to the above snippet −

y1<-sample(LETTERS[1:26],50,replace=TRUE)
y2<-sample(LETTERS[1:26],50,replace=TRUE)
y3<-sample(LETTERS[1:26],50,replace=TRUE)
y3

Output

If you execute all the above given snippets as a single program, it generates the following Output −

[1] "L" "D" "W" "O" "P" "G" "K" "A" "A" "W" "S" "D" "A" "J" "Z" "H" "J" "O" "M"
[20] "A" "R" "L" "T" "J" "M" "D" "L" "W" "L" "C" "E" "H" "H" "S" "U" "J" "J" "R"
[39] "A" "P" "D" "F" "N" "G" "L" "K" "N" "N" "T" "A"

To concatenate elements of equal size vectors add the following code to the above snippet −

y1<-sample(LETTERS[1:26],50,replace=TRUE)
y2<-sample(LETTERS[1:26],50,replace=TRUE)
y3<-sample(LETTERS[1:26],50,replace=TRUE)
y4<-sample(LETTERS[1:26],50,replace=TRUE)
y4

Output

If you execute all the above given snippets as a single program, it generates the following Output −

[1] "Z" "E" "D" "P" "T" "E" "K" "P" "X" "N" "Y" "D" "Z" "U" "A" "H" "J" "J" "G"
[20] "B" "K" "V" "A" "W" "Y" "F" "Q" "E" "J" "S" "V" "S" "I" "M" "H" "U" "F" "D"
[39] "D" "G" "D" "G" "Y" "L" "V" "B" "M" "V" "Y" "E"

To concatenate elements of equal size vectors add the following code to the above snippet −

y1<-sample(LETTERS[1:26],50,replace=TRUE)
y2<-sample(LETTERS[1:26],50,replace=TRUE)
y3<-sample(LETTERS[1:26],50,replace=TRUE)
y4<-sample(LETTERS[1:26],50,replace=TRUE)
c(rbind(y1,y2,y3,y4))

Output

If you execute all the above given snippets as a single program, it generates the following Output −

[1] "V" "J" "L" "Z" "J" "F" "D" "E" "M" "L" "W" "D" "O" "T" "O" "P" "H" "Z"
[19] "P" "T" "N" "I" "G" "E" "D" "C" "K" "K" "K" "M" "A" "P" "H" "X" "A" "X"
[37] "D" "W" "W" "N" "B" "G" "S" "Y" "V" "U" "D" "D" "I" "Q" "A" "Z" "N" "X"
[55] "J" "U" "C" "S" "Z" "A" "U" "I" "H" "H" "U" "E" "J" "J" "E" "F" "O" "J"
[73] "Q" "B" "M" "G" "N" "G" "A" "B" "H" "B" "R" "K" "G" "N" "L" "V" "U" "D"
[91] "T" "A" "I" "R" "J" "W" "B" "W" "M" "Y" "Z" "V" "D" "F" "L" "K" "L" "Q"
[109] "Q" "F" "W" "E" "C" "H" "L" "J" "M" "R" "C" "S" "W" "V" "E" "V" "E" "J"
[127] "H" "S" "Q" "K" "H" "I" "V" "V" "S" "M" "T" "J" "U" "H" "D" "J" "J" "U"
[145] "V" "O" "J" "F" "O" "F" "R" "D" "U" "T" "A" "D" "P" "V" "P" "G" "W" "Y"
[163] "D" "D" "R" "X" "F" "G" "X" "W" "N" "Y" "H" "B" "G" "L" "L" "S" "L" "V"
[181] "E" "J" "K" "B" "I" "M" "N" "M" "X" "D" "N" "V" "B" "Y" "T" "Y" "B" "V"
[199] "A" "E"

Example 3

To concatenate elements of equal size vectors add the following code to the above snippet −

z1<-round(rnorm(50),2)
z1

Output

If you execute all the above given snippets as a single program, it generates the following Output −

[1] -0.07 -0.32 0.88 -0.37 -0.03 1.38 -0.13 -0.80 -0.01 -1.14 0.29 0.32
[13] -0.98 0.18 -2.06 -0.98 -0.54 -0.76 -0.30 -0.30 -0.11 -0.04 1.20 -0.18
[25] -1.00 1.94 -2.42 0.39 0.22 -0.15 0.24 -0.05 1.66 0.78 -0.11 1.06
[37] -0.80 0.78 -1.05 0.94 0.43 -0.35 0.18 -0.07 0.52 0.42 -0.42 -0.89
[49] -0.81 -2.22

To concatenate elements of equal size vectors add the following code to the above snippet −

z1<-round(rnorm(50),2)
z2<-round(rnorm(50),2)
z2

Output

If you execute all the above given snippets as a single program, it generates the following Output −

[1] -1.33 0.09 -0.90 0.34 1.06 -0.05 -0.71 0.39 -0.65 -0.23 0.18 0.10
[13] -0.91 0.48 -0.98 1.77 2.93 1.41 0.50 0.24 -0.56 -0.92 0.44 -0.90
[25] -0.99 0.23 0.63 0.69 0.60 -0.77 -1.14 -1.28 1.04 -1.54 -0.91 1.41
[37] -0.83 0.32 0.18 -0.24 0.31 -0.30 -1.13 -0.33 -0.59 -1.51 -0.53 -0.19
[49] 1.55 0.46

To concatenate elements of equal size vectors add the following code to the above snippet −

z1<-round(rnorm(50),2)
z2<-round(rnorm(50),2)
z3<-round(rnorm(50),2)
z3

Output

If you execute all the above given snippets as a single program, it generates the following Output −

[1] 1.41 0.60 -0.88 -1.24 0.02 -0.29 -0.79 1.74 -0.22 -2.03 0.22 0.12
[13] 0.42 -1.69 -0.81 -1.14 1.20 0.45 1.23 -0.62 0.06 -0.84 -0.78 0.04
[25] 2.29 -0.69 1.05 -0.46 -0.12 0.38 -0.30 1.90 1.22 0.33 0.57 0.43
[37] 0.85 0.18 -1.13 0.75 0.14 -0.76 0.58 -0.51 -0.37 0.54 -1.04 1.31
[49] 1.12 0.70

To concatenate elements of equal size vectors add the following code to the above snippet −

z1<-round(rnorm(50),2)
z2<-round(rnorm(50),2)
z3<-round(rnorm(50),2)
z4<-round(rnorm(50),2)
z4

Output

If you execute all the above given snippets as a single program, it generates the following Output −

[1] 0.22 -0.73 2.12 0.77 -2.08 -0.96 1.54 -1.15 0.32 0.19 0.44 0.14
[13] 0.38 -0.29 -0.50 1.44 0.03 1.02 -0.16 -1.29 -1.62 -0.74 -0.76 -1.43
[25] 0.53 1.25 0.04 1.11 0.41 1.80 -2.40 0.04 -1.25 0.12 -1.49 1.28
[37] 1.39 -0.05 -0.46 -1.26 1.21 -0.32 -0.67 1.90 -0.48 1.26 -0.51 -0.08
[49] -0.02 -1.08

To concatenate elements of equal size vectors add the following code to the above snippet −

z1<-round(rnorm(50),2)
z2<-round(rnorm(50),2)
z3<-round(rnorm(50),2)
z4<-round(rnorm(50),2)
c(rbind(z1,z2,z3,z4))

Output

If you execute all the above given snippets as a single program, it generates the following Output −

[1] -0.07 -1.33 1.41 0.22 -0.32 0.09 0.60 -0.73 0.88 -0.90 -0.88 2.12
[13] -0.37 0.34 -1.24 0.77 -0.03 1.06 0.02 -2.08 1.38 -0.05 -0.29 -0.96
[25] -0.13 -0.71 -0.79 1.54 -0.80 0.39 1.74 -1.15 -0.01 -0.65 -0.22 0.32
[37] -1.14 -0.23 -2.03 0.19 0.29 0.18 0.22 0.44 0.32 0.10 0.12 0.14
[49] -0.98 -0.91 0.42 0.38 0.18 0.48 -1.69 -0.29 -2.06 -0.98 -0.81 -0.50
[61] -0.98 1.77 -1.14 1.44 -0.54 2.93 1.20 0.03 -0.76 1.41 0.45 1.02
[73] -0.30 0.50 1.23 -0.16 -0.30 0.24 -0.62 -1.29 -0.11 -0.56 0.06 -1.62
[85] -0.04 -0.92 -0.84 -0.74 1.20 0.44 -0.78 -0.76 -0.18 -0.90 0.04 -1.43
[97] -1.00 -0.99 2.29 0.53 1.94 0.23 -0.69 1.25 -2.42 0.63 1.05 0.04
[109] 0.39 0.69 -0.46 1.11 0.22 0.60 -0.12 0.41 -0.15 -0.77 0.38 1.80
[121] 0.24 -1.14 -0.30 -2.40 -0.05 -1.28 1.90 0.04 1.66 1.04 1.22 -1.25
[133] 0.78 -1.54 0.33 0.12 -0.11 -0.91 0.57 -1.49 1.06 1.41 0.43 1.28
[145] -0.80 -0.83 0.85 1.39 0.78 0.32 0.18 -0.05 -1.05 0.18 -1.13 -0.46
[157] 0.94 -0.24 0.75 -1.26 0.43 0.31 0.14 1.21 -0.35 -0.30 -0.76 -0.32
[169] 0.18 -1.13 0.58 -0.67 -0.07 -0.33 -0.51 1.90 0.52 -0.59 -0.37 -0.48
[181] 0.42 -1.51 0.54 1.26 -0.42 -0.53 -1.04 -0.51 -0.89 -0.19 1.31 -0.08
[193] -0.81 1.55 1.12 -0.02 -2.22 0.46 0.70 -1.08
raja
Updated on 02-Nov-2021 06:10:10

Advertisements