The Difference Between rbind and cbind in R


Two functions that you will use often in R for a variety of tasks are the rbind and cbind functions.

The rbind function, short for row-bind, can be used to combine vectors, matrices and data frames by rows.

The cbind function, short for column-bind, can be used to combine vectors, matrices and data frames by columns.

Depending on the task that you’d like to perform, one or both of these functions could be useful to use.

The following examples show how to use each of these methods in practice.

Example 1: Use rbind() and cbind() with Vectors

Suppose that we have several vectors that we would like to “bind” together in different ways.

First, we will create the following three vectors:

#create three vectors
vector1 <- c(1, 3, 3, 4, 5)
vector2<- c(7, 7, 8, 3, 2)
vector3 <- c(9, 9, 0, 0, 9)

One way to combine these three vectors into a matrix is by using the rbind() function, which will bind the vectors together by row.

The following example shows how to do so:

#rbind vectors into matrix
my_matrix <- rbind(vector1, vector2, vector3)

#view matrix
my_matrix

        [,1] [,2] [,3] [,4] [,5]
vector1    1    3    3    4    5
vector2    7    7    8    3    2
vector3    9    9    0    0    9

The result is a matrix in each each row of the matrix is one of the original vectors.

The final matrix has 3 rows and 5 columns.

Another way to combine these three vectors into a matrix is by using the cbind() function, which will bind the vectors together by column.

The following example shows how to do so:

#cbind vectors into matrix
my_matrix <- cbind(vector1, vector2, vector3)

#view matrix
my_matrix

     vector1 vector2 vector3
[1,]       1       7       9
[2,]       3       7       9
[3,]       3       8       0
[4,]       4       3       0
[5,]       5       2       9

The result is a matrix in each each column of the matrix is one of the original vectors.

The final matrix has 5 rows and 3 columns.

Notice that the rbind() and cbind() functions are both capable of binding together the vectors into a matrix, but the way they bind them together is either row-wise or column-wise.

Example 2: Use rbind() and cbind() with Data Frames

Suppose that we have several data frames that we would like to “bind” together in different ways.

First, we will create the following two data frames in R:

#create first data frame
df1 <- data.frame(team=c('A', 'A', 'B', 'B', 'C'),
                  points=c(22, 25, 30, 43, 19))

df1

  team points
1    A     22
2    A     25
3    B     30
4    B     43
5    C     19

#create second data frame
df2 <- data.frame(team=c('D', 'D', 'E', 'F', 'F'),
                  points=c(11, 36, 29, 22, 30))

df2

  team points
1    D     11
2    D     36
3    E     29
4    F     22
5    F     30

One way to combine these two data frames into one data frame is by using the rbind() function, which will bind the data frames together by row.

The following example shows how to do so:

#rbind data frames into one data frame
new_df <- rbind(df1, df2)

#view data frame
new_df

   team points
1     A     22
2     A     25
3     B     30
4     B     43
5     C     19
6     D     11
7     D     36
8     E     29
9     F     22
10    F     30

The result is a data frame with 10 rows and 2 columns.

Another way to combine these two data frames into one data frame is by using the cbind() function, which will bind the data frames together by column.

The following example shows how to do so:

#cbind data frames into one data frame
new_df <- cbind(df1, df2)

#view data frame
new_df

  team points team points
1    A     22    D     11
2    A     25    D     36
3    B     30    E     29
4    B     43    F     22
5    C     19    F     30

The result is a data frame with 4 columns and 5 rows.

Additional Resources

The following tutorials explain how to perform other common operations in R:

R: How to Use rbindlist
R: How to Use cbind with Vectors of Different Lengths
R: How to Rename Columns When Using cbind

Featured Posts

Leave a Reply

Your email address will not be published. Required fields are marked *