When doing data science, it is common to organize data in a matrix. In r programming, the first thought may be to use the matrix function. However, the r language offers other functions that are handy when working with multiple vectors.

### Description

if you are looking to combine several vectors into a matrix the rbind and cbind functions are a handy choice for accomplishing the task. The first uses each vector to produce a row and the second uses them to produce a column. The format of both functions is the same but they each produce a different format. It is this difference in format that makes each of them distinct. You decide which one you want to use based on whether you want your data displayed vertically or horizontally.

### Explanation

When you use these two functions the way you enter your data is the same, with the format of _bind(vector list). Now from the user’s perspective, there are only two differences between these two functions. The first difference is that one starts with an “r” and the other starts with a “c”. With these functions, rbind stands for row bind and cbind stands for column bind. This leads to the other difference, which is that one uses the vectors to bind rows and the other uses each vector to produce a column. The consequence is that deciding whether to use row bind or column bind is entirely a question of formatting.

### Examples

Here we have three examples illustrating 3 different ways of using the row bind and column bind functions.

> M1 = cbind(c(1, 2, 3, 4, 5, 6, 7),

+ c(1, 2, 3, 4, 1, 2, 3),

+ c(“A”, “B”, “C”, “D”, “E”, “F”, “G”),

+ c(“A”, “B”, “C”, “D”, “A”, “B”, “C”))

>

> M2 = rbind(c(1, 2, 3, 4, 5, 6, 7),

+ c(1, 2, 3, 4, 1, 2, 3),

+ c(“A”, “B”, “C”, “D”, “E”, “F”, “G”),

+ c(“A”, “B”, “C”, “D”, “A”, “B”, “C”))

> M1

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

[1,] “1” “1” “A” “A”

[2,] “2” “2” “B” “B”

[3,] “3” “3” “C” “C”

[4,] “4” “4” “D” “D”

[5,] “5” “1” “E” “A”

[6,] “6” “2” “F” “B”

[7,] “7” “3” “G” “C”

> M2

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

[1,] “1” “2” “3” “4” “5” “6” “7”

[2,] “1” “2” “3” “4” “1” “2” “3”

[3,] “A” “B” “C” “D” “E” “F” “G”

[4,] “A” “B” “C” “D” “A” “B” “C”

This example illustrates the use of these two functions with unnamed internal vectors. Note that the rbind function places into vectors into the row while the cbind function places them into columns.

> M1 = cbind(A = c(1, 2, 3, 4, 5, 6, 7),

+ B = c(1, 2, 3, 4, 1, 2, 3),

+ C = c(“A”, “B”, “C”, “D”, “E”, “F”, “G”),

+ D = c(“A”, “B”, “C”, “D”, “A”, “B”, “C”))

>

> M2 = rbind(A = c(1, 2, 3, 4, 5, 6, 7),

+ B = c(1, 2, 3, 4, 1, 2, 3),

+ C = c(“A”, “B”, “C”, “D”, “E”, “F”, “G”),

+ D = c(“A”, “B”, “C”, “D”, “A”, “B”, “C”))

>

> M1

A B C D

[1,] “1” “1” “A” “A”

[2,] “2” “2” “B” “B”

[3,] “3” “3” “C” “C”

[4,] “4” “4” “D” “D”

[5,] “5” “1” “E” “A”

[6,] “6” “2” “F” “B”

[7,] “7” “3” “G” “C”

> M2

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

A “1” “2” “3” “4” “5” “6” “7”

B “1” “2” “3” “4” “1” “2” “3”

C “A” “B” “C” “D” “E” “F” “G”

D “A” “B” “C” “D” “A” “B” “C”

In this example, the two functions use internally named vectors and the consequence is that the rbind function produces row names while the cbind function produces column names.

> A = c(1, 2, 3, 4, 5, 6, 7)

> B = c(1, 2, 3, 4, 1, 2, 3)

> C = c(“A”, “B”, “C”, “D”, “E”, “F”, “G”)

> D = c(“A”, “B”, “C”, “D”, “A”, “B”, “C”)

>

> M1 = cbind(A, B, C, D)

> M2 = rbind(A, B, C, D)

>

> M1

A B C D

[1,] “1” “1” “A” “A”

[2,] “2” “2” “B” “B”

[3,] “3” “3” “C” “C”

[4,] “4” “4” “D” “D”

[5,] “5” “1” “E” “A”

[6,] “6” “2” “F” “B”

[7,] “7” “3” “G” “C”

> M2

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

A “1” “2” “3” “4” “5” “6” “7”

B “1” “2” “3” “4” “1” “2” “3”

C “A” “B” “C” “D” “E” “F” “G”

D “A” “B” “C” “D” “A” “B” “C”

In this example, the two functions use externally named vectors and the consequence is that the rbind function produces row names while the cbind function produces column names. This is the simplest way to use these functions.

### Application

The primary application of these two functions is a situation where you have a list of objects in a group, and you want to put them in a matrix with the name of the group either as a column name or a row name. A good example of this would be a list of cars that you can buy at local dealers. Whether you use row bind or column bind depends upon whether you want the dealer’s name to be on the column or the row.

If you are trying to convert a list of vectors into a matrix row bind and column bind are the functions to use. While they are used in the same manner, they supply different formats for your matrix.