Binding by Row or Column

The simplest way in R to join two tables is just stack them one on top of the other or side by side. However, this will only work if the tables contain exactly the same number of columns with the same column names or if they are exactly the same length.

Bind Rows

If you have two datasets that contain all the same columns and you want to stack them together, you will use the command rbind(). Inside this function you will list the tables that you want to combine and it will stack them in the order that you list. You can list as many objects in rbind() as you want, providing they all have the same column format. We’ll create two tables to illustrate:

Note that the two data frames are not of equal length, y has more rows, but they contain the same columns. Now we’ll bind them with rbind():

Simple! Take note that the following table cannot be added to table x:

Why does this give you an error?

Because R is case sensitive and to use rbind() the column names need to match exactly.

Similarly, this table will also give you an error when you try to combine it with x:

This time even though col1 and col2 match there is no corresponding col3, so this would make columns of unequal lengths if combined: columns 1 and 2 would contain five rows, while column 3 would contain only two.

Bind Columns

Binding by column works the same way using the command cbind(). This time we are adding unique columns together, so they need to be the same number of rows for the combination to work. See if you can add to the code to create a table to join to x.

As long as you add a table with three rows, you can have as many columns as you’d like.

For either rbind() or cbind(), you can combine numerics and strings together, just note that if you combine them in the same column, this will change the data type of the column to be a character.

Use the command glimpse() to check the data type for all columns in both x and combo.

<dbl> indicates the data type “double” which is numeric and <chr> indicates a character data type.


Those are the simplest cases for adding tables together, but they only work for very similar tables. Now we’ll learn some more advanced ways to combine.