How to combine array of vectors in R?

R ProgrammingServer Side ProgrammingProgramming

To combine array of vectors we can use rbind function. For example, if we have multiple vectors say x, y, z of same size or different sizes but the total number of elements are even then we can use rbind(x,y,z) to combine those vectors. Check out the examples to understand how it works.

Example1

 Live Demo

x1<−rbind(rep(1,5),rep(2,5),rep(3,5))
x1

Output

   [,1] [,2] [,3] [,4] [,5]
[1,]  1   1    1    1   1
[2,]  2 2 2 2 2
[3,] 3 3 3 3 3

Example2

 Live Demo

x2<−rbind(rep(4,5),rep(5,5),rep(6,5),rep(7,5),rep(8,5),rep(9,5),rep(10,5),rep(11,5),rep(12,5),rep(13,5),rep(14,5),rep(15,5))
x2

Output

   [,1] [,2] [,3] [,4] [,5]
[1,] 4    4    4    4   4
[2,] 5    5    5    5   5
[3,] 6    6    6    6   6
[4,] 7    7    7    7   7
[5,] 8    8    8    8   8
[6,] 9    9    9    9   9
[7,] 10   10   10   10  10
[8,] 11   11   11   11  11
[9,] 12   12   12   12  12
[10,] 13   13  13   13  13
[11,] 14   14   14 14   14
[12,] 15   15   15  15   15

Example3

 Live Demo

x3<−rbind(rep(16,5),rep(17,5),rep(18,5),rep(19,5),rep(20,5),rep(21,5),rep(22,5),rep(23,5),rep(24,5),rep(25,5),rep(26,5),rep(27,5),rep(28,5),rep(29,5),rep(30,5))
x3

Output

[,1] [,2] [,3] [,4] [,5]
[1,] 16 16 16 16 16
[2,] 17 17 17 17 17
[3,] 18 18 18 18 18
[4,] 19 19 19 19 19
[5,] 20 20 20 20 20
[6,] 21 21 21 21 21
[7,] 22 22 22 22 22
[8,] 23 23 23 23 23
[9,] 24 24 24 24 24
[10,] 25 25 25 25 25
[11,] 26 26 26 26 26
[12,] 27 27 27 27 27
[13,] 28 28 28 28 28
[14,] 29 29 29 29 29
[15,] 30 30 30 30 30

Example4

x<−rbind(x1,x2,x3)
x

Output

   [,1] [,2] [,3] [,4] [,5]
[1,] 1 1 1 1 1
[2,] 2 2 2 2 2
[3,] 3 3 3 3 3
[4,] 4 4 4 4 4
[5,] 5 5 5 5 5
[6,] 6 6 6 6 6
[7,] 7 7 7 7 7
[8,] 8 8 8 8 8
[9,] 9 9 9 9 9
[10,] 10 10 10 10 10
[11,] 11 11 11 11 11
[12,] 12 12 12 12 12
[13,] 13 13 13 13 13
[14,] 14 14 14 14 14
[15,] 15 15 15 15 15
[16,] 16 16 16 16 16
[17,] 17 17 17 17 17
[18,] 18 18 18 18 18
[19,] 19 19 19 19 19
[20,] 20 20 20 20 20
[21,] 21 21 21 21 21
[22,] 22 22 22 22 22
[23,] 23 23 23 23 23
[24,] 24 24 24 24 24
[25,] 25 25 25 25 25
[26,] 26 26 26 26 26
[27,] 27 27 27 27 27
[28,] 28 28 28 28 28
[29,] 29 29 29 29 29
[30,] 30 30 30 30 30

Example5

 Live Demo

s1<−rbind(rep("A",10),rep("B",10),rep("C",10),rep("D",10),rep("E",10),rep("F",10),rep("G",10),rep("H",10),rep("I",10),rep("J",10),rep("K",10),rep("L",10),rep("M",10),rep("N",10),rep("O",10))
s1

Output

[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] "A" "A" "A" "A" "A" "A" "A" "A" "A" "A"
[2,] "B" "B" "B" "B" "B" "B" "B" "B" "B" "B"
[3,] "C" "C" "C" "C" "C" "C" "C" "C" "C" "C"
[4,] "D" "D" "D" "D" "D" "D" "D" "D" "D" "D"
[5,] "E" "E" "E" "E" "E" "E" "E" "E" "E" "E"
[6,] "F" "F" "F" "F" "F" "F" "F" "F" "F" "F"
[7,] "G" "G" "G" "G" "G" "G" "G" "G" "G" "G"
[8,] "H" "H" "H" "H" "H" "H" "H" "H" "H" "H"
[9,] "I" "I" "I" "I" "I" "I" "I" "I" "I" "I"
[10,] "J" "J" "J" "J" "J" "J" "J" "J" "J" "J"
[11,] "K" "K" "K" "K" "K" "K" "K" "K" "K" "K"
[12,] "L" "L" "L" "L" "L" "L" "L" "L" "L" "L"
[13,] "M" "M" "M" "M" "M" "M" "M" "M" "M" "M"
[14,] "N" "N" "N" "N" "N" "N" "N" "N" "N" "N"
[15,] "O" "O" "O" "O" "O" "O" "O" "O" "O" "O"

Example6

 Live Demo

s2<−rbind(rep("P",10),rep("Q",10),rep("R",10),rep("S",10),rep("T",10),rep("U",10),rep("V",10),rep("W",10),rep("W",10),rep("Y",10),rep("Z",10))
> s2

Output

[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] "P" "P" "P" "P" "P" "P" "P" "P" "P" "P"
[2,] "Q" "Q" "Q" "Q" "Q" "Q" "Q" "Q" "Q" "Q"
[3,] "R" "R" "R" "R" "R" "R" "R" "R" "R" "R"
[4,] "S" "S" "S" "S" "S" "S" "S" "S" "S" "S"
[5,] "T" "T" "T" "T" "T" "T" "T" "T" "T" "T"
[6,] "U" "U" "U" "U" "U" "U" "U" "U" "U" "U"
[7,] "V" "V" "V" "V" "V" "V" "V" "V" "V" "V"
[8,] "W" "W" "W" "W" "W" "W" "W" "W" "W" "W"
[9,] "W" "W" "W" "W" "W" "W" "W" "W" "W" "W"
[10,] "Y" "Y" "Y" "Y" "Y" "Y" "Y" "Y" "Y" "Y"
[11,] "Z" "Z" "Z" "Z" "Z" "Z" "Z" "Z" "Z" "Z"

Example7

S<−rbind(s1,s2)
S

Output

[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] "A" "A" "A" "A" "A" "A" "A" "A" "A" "A"
[2,] "B" "B" "B" "B" "B" "B" "B" "B" "B" "B"
[3,] "C" "C" "C" "C" "C" "C" "C" "C" "C" "C"
[4,] "D" "D" "D" "D" "D" "D" "D" "D" "D" "D"
[5,] "E" "E" "E" "E" "E" "E" "E" "E" "E" "E"
[6,] "F" "F" "F" "F" "F" "F" "F" "F" "F" "F"
[7,] "G" "G" "G" "G" "G" "G" "G" "G" "G" "G"
[8,] "H" "H" "H" "H" "H" "H" "H" "H" "H" "H"
[9,] "I" "I" "I" "I" "I" "I" "I" "I" "I" "I"
[10,] "J" "J" "J" "J" "J" "J" "J" "J" "J" "J"
[11,] "K" "K" "K" "K" "K" "K" "K" "K" "K" "K"
[12,] "L" "L" "L" "L" "L" "L" "L" "L" "L" "L"
[13,] "M" "M" "M" "M" "M" "M" "M" "M" "M" "M"
[14,] "N" "N" "N" "N" "N" "N" "N" "N" "N" "N"
[15,] "O" "O" "O" "O" "O" "O" "O" "O" "O" "O"
[16,] "P" "P" "P" "P" "P" "P" "P" "P" "P" "P"
[17,] "Q" "Q" "Q" "Q" "Q" "Q" "Q" "Q" "Q" "Q"
[18,] "R" "R" "R" "R" "R" "R" "R" "R" "R" "R"
[19,] "S" "S" "S" "S" "S" "S" "S" "S" "S" "S"
[20,] "T" "T" "T" "T" "T" "T" "T" "T" "T" "T"
[21,] "U" "U" "U" "U" "U" "U" "U" "U" "U" "U"
[22,] "V" "V" "V" "V" "V" "V" "V" "V" "V" "V"
[23,] "W" "W" "W" "W" "W" "W" "W" "W" "W" "W"
[24,] "W" "W" "W" "W" "W" "W" "W" "W" "W" "W"
[25,] "Y" "Y" "Y" "Y" "Y" "Y" "Y" "Y" "Y" "Y"
[26,] "Z" "Z" "Z" "Z" "Z" "Z" "Z" "Z" "Z" "Z"
raja
Published on 17-Oct-2020 12:30:13
Advertisements