In this article we will work on learning how to bind two rows in R using rbind() command.
Theory
Did you ever come across a problem of binding two rows into a single table?
I’m sure you did! Especially in data analytics and data science, we often produce results in a new table (or a data frame) and the end goal is to combine it all in one table to draw meaningful insights from the results.
For example, think about running a store, and you are working with a POS (point of sale) system. You decide to look up customers by their ID. But you can only look up one at a time.
Here is the problem: each “run” of the system will create a single row. But what you are interested in is having those two rows in one table.
Assume your output will contain the following information: ID, Name, City, and Sales (amount the customer purchased from your store).
We know there are two customers who purchase more than others and their IDs are 1111 and 1112.
We would like to extract the data and have it in a single table to pass it on to the sales department, who then will contact them with some promotional offer.
Sounds like a real life example, isn’t it?
Therefore what we will need to do (since the system only provides us with information on one customer at a time) is to bind the two rows together into a single table.
Below I will show an example of the usage of popular R base command rbind().
Application
Below are the steps we are going to take to make sure we do master the skill of binding two rows in R:
- Basic rbind() command description
- Creating two sample tables in R
- Binding two rows in R
Part 1. Basic rbind() command description
The short theoretical explanation of the function is the following:
rbind(object1, object2, …)
Here, “object” refers to either a table, or a data frame, or any other data structure you would like to bind.
In our example, there will be two objects “user1111” and “user1112”. But you can always bind more than two objects.
Part 2. Creating two sample tables in R
For the purpose of this article we will create two data frames. First data frame will be information on client ID “1111”. Second data frame will be information on client ID “1112”.
You can create these sample data frames using the following code:
user1111<-data.frame(id = c("1111"), name = c("John Smith"), city = c("Boston"), sales = c("$88,900"))
user1112<-data.frame(id = c("1112"), name = c("Erik Johnson"), city = c("Buffalo"), sales = c("$74,500"))
Now, we can take a look at the two data frames we just created:
View(user1111)
View(user1112)
Note: in this article I create my own datasets. If you have your own in a csv or excel files, you can follow the same procedure to arrive at the result.
Part 3. Binding two rows in R
Our goal now is to create a new data frame (table) "report" where we will have both "user1111" and "user1112" binded.
This is the part of the article where we will see an applied example on how to bind two rows in R.
As discussed above, "user1111" and "user1112" will be our "objects" for the rbind() command.
At this point, we are all set to bind two rows in R!
You can do it using the following code:
report<-rbind(user1111, user1112)
You can take a look at the edited table by using this command:
View(report)
If you are interested to learn more about data manipulation in R, you can find more articles here.