You are currently viewing R Append Single or Multiple Rows to Data Frame

To append a single row or multiple rows to the R data frame use either rbin(), add_row() from the tidyverse package, or rows_insert() from the dplyr package. In this article, I will use these methods to explain appending rows to the existing data frame.

1. Quick Example of Appending Rows to Data Frame

Following are quick examples of appending rows to a Data Frame.


#append row to end of data frame 
#using rbind()
vecRow <- c(15, 'Kumar')
df1 <- rbind(df1,vecRow)

#using nrow()
df1[nrow(df1) + 1,] = vecRow

#using tidyverse to add row
library(tidyverse)
df1 <- df1 %>% add_row(id='20',
                       name='Ann',
                       .before=2)

library(dplyr)
df2 = data.frame(id = '25', name = 'Lyn')
df2 <- df1 %>% 
  rows_insert(df2)

Let’s create an R DataFrame, run these examples and explore the output. If you already have data in CSV you can easily import CSV files to R DataFrame. Also, refer to Import Excel File into R.


# Create DataFrame
df1 <- data.frame(id = c(10,11),
                  name = c('sai','ram'))
df1

Yields below output.

r append rows

2. Using rbin() to Append Rows to R Data Frame

Use rbin() function from the R base package to append rows to the data frame in R. This function appends all rows at the end of the data frame and returns the result in a new data frame. This function requires the data frame and the row you are appending to have the same columns.

In the below example, I have assigned the result to the data frame df1 object.


#append row to end of data frame 
#using rbind()
vecRow <- c(15, 'Kumar')
df1 <- rbind(df1,vecRow)
df1

Yields below output.

r append multiple rows

Alternatively, you can also use nrow() function. This approach actually updates the data frame with the row you inserted.

#append row to end of data frame #using nrow() df1[nrow(df1) + 1,] = vecRow df1

3. Using tidyverse Package to Append Rows

You can also use the add_row() function from the tidyverse package to append rows to the R data frame. By using this function you can add a row at any specific row index. The below example inserts the row at the second position.

In order to use this, you have to install package tidyverse first using install.packages('tidyverse') and load it using library(tidyverse).


#using tidyverse to add row
library(tidyverse)
df1 <- df1 %>% add_row(id='20',
                       name='Ann',
                       .before=2)
df1

4. Using dplyr Package to Append Rows

Finally, let’s use the rows_insert() function from the dplyr package to append a row to the R data frame. In order to use this, you have to install package dplyr first using install.packages('dplyr') and load it using library(dplyr). In the below example, %>% is a pipe or infix operator.


# Using dplyr
library(dplyr)
df2 = data.frame(id = '25', name = 'Lyn')
df2 <- df1 %>% 
  rows_insert(df2)
df2

5. Append Multiple Rows to Data Frame

By using the approaches explained above you can also append multiple rows to the R data frame. Below is an example.


# Create DataFrame
df1 <- data.frame(id = c(10,11),
                  name = c('sai','ram'))
df1

# Create DataFrame
df2 <- data.frame(id = c(16,17),
                  name = c('Don','Lin'))

df2

#Append multiple rows to the data frame
#using rbind()
df3 <- rbind(df1,df2)
df3

For other approaches refer to append data frames in R, here I have covered several examples of appending data frames into a single data frame.

6. Conclusion

In this article, you have learned how to append single row and multiple rows to the R data frame by using rbin() function from the R base package, add_row() from the tidyverse package, and rows_insert() from the dplyr package.

Related Articles

Naveen Nelamali

Naveen Nelamali (NNK) is a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, He has honed his expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. Naveen journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. In this blog, he shares his experiences with the data as he come across. Follow Naveen @ LinkedIn and Medium