Add New Row at Specific Index Position to Data Frame in R (Example)

 

This tutorial shows how to insert a new data frame row at a certain row index in the R programming language.

The post is structured as follows:

Sound good? Here’s the step-by-step process…

 

Construction of Example Data

We’ll use the following data as basement for this R tutorial:

data <- data.frame(x1 = 1:5,              # Create example data
                   x2 = 11:15,
                   x3 = 21:25)
data                                      # Print example data
#   x1 x2 x3
# 1  1 11 21
# 2  2 12 22
# 3  3 13 23
# 4  4 14 24
# 5  5 15 25

As you can see based on the previously shown output of the RStudio console, our example data consists of five rows and three columns.

Let’s also create a vector object that we will use as new row:

new_row <- c(9, 99, 999)                  # Create example row
new_row                                   # Print example row
# 9  99 999

Our new row vector contains three numeric values.

 

Example: Adding New Row at Certain Position of Data Frame Using rbind() Function

In this Example, I’ll show how to insert a new row to a data frame at a specific row index using the rbind function.

Consider the following R code:

data_new <- rbind(data[1:3, ],            # Applying rbind function
                  new_row,
                  data[- (1:3), ])
data_new                                  # Print new data frame
#    x1 x2  x3
# 1   1 11  21
# 2   2 12  22
# 3   3 13  23
# 4   9 99 999
# 41  4 14  24
# 5   5 15  25

As you can see, we have added our new row after the third index position of our data. You may modify the value 3 in the previous R code to use other row-indices.

The previous R code messed up the row names of our data frame. We may modify the row names of our new data to range from 1 to the number of rows of our data as shown below:

rownames(data_new) <- 1:nrow(data_new)    # Modify row names
data_new                                  # Print data with updated row names
#   x1 x2  x3
# 1  1 11  21
# 2  2 12  22
# 3  3 13  23
# 4  9 99 999
# 5  4 14  24
# 6  5 15  25

Note that the rbind function can be relatively slow when applied to large data sets. If you are looking for faster solutions, please have a look here.

 

Video, Further Resources & Summary

I have recently published a video on my YouTube channel, which explains the examples of this article. You can find the video below:

 

 

Furthermore, you may want to have a look at the other articles of this homepage:

 

On this page, I explained how to append new rows at a specific position of a data frame in R programming. Let me know in the comments, in case you have further questions. Furthermore, please subscribe to my email newsletter in order to get regular updates on new tutorials.

 

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.

The maximum upload file size: 2 MB. You can upload: image. Drop file here

Top