R Append Data Frames

  • Post author:
  • Post category:R Programming
  • Post last modified:December 23, 2022

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

NNK

SparkByExamples.com is a Big Data and Spark examples community page, all examples are simple and easy to understand and well tested in our development environment Read more ..

Leave a Reply