The binding or combining of the rows is very easy with the rbind() function in R.
rbind() stands for row binding. In simpler terms joining of multiple rows to form a single batch. It may include joining two data frames, vectors, and more.
This article will talk about the uses and applications of rbind() function in R programming.
Without wasting much time, let’s roll in to the topic!!!
rbind(): The rbind or the row bind function is used to bind or combine the multiple group of rows together.
Where:
The idea of binding or combing the rows of multiple data frames is highly beneficial in data manipulation.
The below diagram will definitely get you the idea of working the rbind() function.
You can see that how rows of different data frames will bound/combined by the rbind() function.
As you know that rbind() function in R used to bind the rows of different groups of data.
In this section, let’s try to construct a simple data frames and bind them using rbind() function.
The above code will construct a simple data frame presenting student details and names.
Well, now we have a dataframe of 5 rows. Let’s create another data frame.
Well, now we have 2 data frames of different row counts (df1 and df2). Let’s use the rbind() function to bind the above 2 data frames into a single data frame.
Let’s see how it works.
You won’t believe that the whole binding process will require just a line of code.
The resultant data frame will be a bonded version of both data frames as shown in the above output.
Well, in the previous section, we have combined the two row groups together.
In this section, we are going to combine two data sets together using the rbind function in R.
Oh wait, what happened? Why the function is throwing an error?
If you read the lines in the error, it is stating that the number of columns is not matching.
We have 2 columns in ‘df1’ and 3 columns in ‘df2’.
Worry not! we have got the bind_rows() function which will assist us in these scenarios.
bind_rows() is a function which is a part of dplyr package. We need to import the dplyr package first to execute this function.
We are using the same data frames present in the above section i.e df1 and df2. Let’s see how it works.
you can now see that the bind_rows function has combined these two unequal datasets in terms of columns. The empty spaces will be marked as <NA>.
In this section, we will be looking into the binding of two entire data sets in R.
Let’s see how it works.
We are going to use the BOD data set as it has only 6 rows and also you can easily observe the bound rows.
BOD dataset has 6 rows. As we are passing the data twice, the rbind() function will bind the same rows as shown above.
And also don’t forget that you have bind_rows() function as well.
These are the examples which clearly shows the working and applications of the bind() and bind_rows functions.
I hope these illustrations helped you in understanding these functions.
In this section, we will be focusing on binding multiple (more than 2) row groups using the function rbind(). Let’s see how it works.
Observe, how all three datasets were bonded or combined by the bind_rows() function in R. This is the beauty of bind_rows() function.
These 2 functions have endless applications in data manipulation in R programming.
The rbind() function in R and the bind_rows() function are the most useful functions when it comes to data manipulation.
You can easily bind two data frames of the same column count using rbind() function.
In the same way, if the data frames have unequal column counts, you can use the bind_rows() function along with dplyr package.
Well, That’s all for now, Happy binding!!!
More read: R documentation
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.