You are currently viewing R Append Data Frames

I will explain the different append operations you perform on the R data frame. For example, we are often required to append two data frames into a single, append column and row to a data frame.

1. Quick Examples of Append Data Frames

Following are quick examples of append operations on DataFrame.


# Below are quick examples

#append data frames
#using rbind()
df3 <- rbind(df1,df2)

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

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

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

# append column
df2$state <- c('CA','NY','NJ','UT')

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

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

Yields below output.

r append dataframes

2. R Append Two Data Frames into a Single Data Frame

To append data frames in R, use the rbin() function. This function appends all records from the second data frame at the end of the first data frame. and the rbind() function requires the data frames you are trying to append to have the same columns.

It takes the data frames as arguments, appends records, and finally returns the new data frame with combined records.


# Append data frames
df3 <- rbind(df1,df2)
df3

Yields below output.

r append two data frames

3. Append Row to Data Frame

Another append operation you can perform on the R data frame is, to append rows to the data frame (single or multiple records from a list or vector toward the end). In the below example, c() is used to create a vector, and added this vector as a row to the data frame. you can also add a list as a row to the data frame using the same function.


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

Yields below output.

r append row data frame

Alternatively, you can also use the nrow() function to append the row to the data frame. By using this approach you can append the data frame to an existing dataframe.


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

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

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


# Using tidyverse
library(tidyverse)
df2 <- df2 %>% add_row(id='20',
                       name='Ann',
                       .before=2)
df2

After running the above two examples, you will have the df2 data frame as below.

4. Append Column to Data Frame

To append columns to the data frame use the df with $ notation.


#append column to the data frame
df2$state <- c('CA','NY','NJ','UT')
df2

5. Conclusion

In this article, you have learned different append operations on the R data frames. using the rbind() you can append two data frames into a single data frame and also append rows to the data frame. Also used nrow() and add_row() from tidyverse package to append row.

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