Add New Row to Data Frame in R (2 Examples)

 

In this article you’ll learn how to append a new row to a data frame in the R programming language.

The table of content looks as follows:

Let’s dive into it.

 

Creation of Example Data

Let’s create some data that we can use in the examples later on. First, we are creating a data frame in R:

data <- data.frame(x1 = 1:4,               # Create example data
                   x2 = 4:1,
                   x3 = 1)
data                                       # Print example data
#   x1 x2 x3
# 1  1  4  1
# 2  2  3  1
# 3  3  2  1
# 4  4  1  1

Our data frame consists of four rows and three numeric variables.

Furthermore, we have to create a vector that we can add as new row to our data frame:

new_row <- c(77, 88, 99)                   # Create example row
new_row                                    # Print example row
# 77 88 99

Our example vector consists of three numeric values. Note that the length of this vector has to be the same length as the number of columns in our data frame (i.e. three) and that the data class of the vector needs to be the same as the data class of our variables (i.e. numeric).

Now, we are set up. Let’s move on to the examples…

 

Example 1: Add Row to Data Frame Using rbind Function

Example 1 explains how to append a new row to a data frame with the rbind function. Within the rbind function, we have to specify the name of our data frame (i.e. data) as well as the name of our vector (i.e. new_row):

data1 <- rbind(data, new_row)              # Apply rbind function
data1                                      # Print updated data
#   x1 x2 x3
# 1  1  4  1
# 2  2  3  1
# 3  3  2  1
# 4  4  1  1
# 5 77 88 99

As you can see based on the output of the RStudio console, we just created a new data object containing our original data plus the vector.

 

Example 2: Add Row to Data Frame by Number of Rows

Another alternative for appending rows to data frames is based on the number of rows of our data frame. By adding + 1 to the number of rows (computed by the nrow function), we can specify that we want to add our vector to the bottom of our data frame:

data2 <- data                              # Replicate data
data2[nrow(data) + 1, ] <- new_row         # Add new row
data2                                      # Print updated data
#   x1 x2 x3
# 1  1  4  1
# 2  2  3  1
# 3  3  2  1
# 4  4  1  1
# 5 77 88 99

The output of the previous R code is exactly the same as in Example 1. Whether you use the rbind function or the nrow function doesn’t really make a difference – it’s a matter of taste.

 

Video, Further Resources & Summary

Do you want to learn more about merging data in R? Then you could watch the following video that I have published on my YouTube channel. In the video, I explain the topics of this tutorial.

 

 

Furthermore, you could have a look at the related articles of this homepage. I have published numerous articles on binding data inputs together.

 

Summary: This article showed how to bind a new row to a data matrix in R programming. In case you have further comments or questions, please tell me about it in the comments section.

 



Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top