R Advanced

Appending Single and Multiple Rows to a Data Frame in R

In the real world, data is not static. You need to keep updating it or adding new data to an existing data set. As new data becomes available, you need to add it to the existing data frame to maintain an up-to-date record.

Here are four ways to append rows to a data frame in R:

  1. Using dplyr::bind_rows() (Efficient Way)
  2. Using rbind()
  3. Using nrow()
  4. Using dplyr::add_row()

Method 1: Using dplyr::bind_rows()

The most efficient way to append a single or multiple rows to a data frame is to use the “dplyr::bind_rows()” function. This function accepts a data frame and the rows to be appended and returns the combined data frame.

Appending single row

When appending a single row, the data frame is a data frame with a single row. Pass that data frame to the bind_rows() function as a second argument.

library(dplyr)

df <- data.frame(
  name = c("Krunal", "Ankit", "Dhaval"),
  score = c(85, 90, 78),
  subject = c("Math", "Science", "History"),
  class = c(10, 11, 12)
)

new_row <- data.frame(
  name = "Niva",
  score = 99,
  subject = "Physics",
  class = 12
)
# Appending a single row using dplyr::bind_rows()
appended_df <- bind_rows(df, new_row)

print(appended_df)

Output

In the above output, you can see that the output data frame contains an appended row.

Appending multiple rows

Appending multiple rows means appending a data frame with multiple rows to your primary data frame, which the bind_rows() function efficiently does.

library(dplyr)

df <- data.frame(
  name = c("Krunal", "Ankit", "Dhaval"),
  score = c(85, 90, 78),
  subject = c("Math", "Science", "History"),
  class = c(10, 11, 12)
)

multiple_rows <- data.frame(
  name = c("Niva", "Vidu", "Mansi"),
  score = c(99, 95, 90),
  subject = c("Physics", "Maths", "Biology"),
  class = c(12, 11, 12)
)
# Appending multiple rows using dplyr::bind_rows()
appended_df <- bind_rows(df, multiple_rows)

print(appended_df)

Output

You can see that in just one go, and we added multiple rows to an existing data frame using the dplyr::bind_rows() function effortlessly.

This approach is constructive when working with multiple data sources and files where column structures might vary.

Pros

  1. The bind_rows() method handles column mismatching by filling missing values with NA.
  2. It can handle lists of data frames effectively and combines multiple data frames.

Cons

  1. It requires installing and loading an external tidyverse or dplyr package.

Method 2: Using rbind()

The rbind() function is a base R function that can add single or multiple columns to a data frame, but column names and the number of columns must be the same.

Appending single row

The rbind() function works the same as the bind_rows() function. It accepts the first argument as the primary data frame and the second argument as a data frame with a single row, and it will bind both data frames row-wise and return a combined single data frame.

df <- data.frame(
  name = c("Krunal", "Ankit", "Dhaval"),
  score = c(85, 90, 78),
  subject = c("Math", "Science", "History"),
  class = c(10, 11, 12)
)

single_row <- data.frame(
  name = "Niva",
  score = 99,
  subject = "Physics",
  class = 12
)
# Appending a single row using rbind() function
combined_df <- rbind(df, single_row)

print(combined_df)

Output

Appending multiple rows

The process is the same: pass the first argument as your primary data frame and the second argument as a data frame with multiple rows. The rbind() function will combine both data frames row-wise and return a single combined data frame.

df <- data.frame(
  name = c("Krunal", "Ankit", "Dhaval"),
  score = c(85, 90, 78),
  subject = c("Math", "Science", "History"),
  class = c(10, 11, 12)
)

multiple_rows <- data.frame(
  name = c("Niva", "Vidu", "Mansi"),
  score = c(99, 95, 90),
  subject = c("Physics", "Maths", "Biology"),
  class = c(12, 11, 12)
)
# Appending multiple rows using rbind()
appended_df <- rbind(df, multiple_rows)

print(appended_df)

Output

Pros

  1. It is fast and efficient when it comes to matching columns.
  2. It requires no external package.

Cons

  1. It strictly requires identical column names and types to be combined.
  2. It does not handle missing columns or different column orders gracefully.

Method 3: Using nrow()

The main functionality of the nrow() function is to return the number of rows of a data frame, but you can use it to append a row in the data frame with indexing.

df <- data.frame(
  name = c("Krunal", "Ankit", "Dhaval"),
  score = c(85, 90, 78),
  subject = c("Math", "Science", "History"),
  class = c(10, 11, 12)
)

new_row <- data.frame(
  name = "Niva",
  score = 99,
  subject = "Physics",
  class = 12
)
# Appending a single row using nrow() function
df[nrow(df) + 1, ] <- new_row

print(df)

Output

You can see from the above figure that we are using the nrow() function to get the length of the data frame and add + 1 (last index) to append a new row at the end of the data frame. 

Using indexing, you can append a row to any index.

Pros

  1. It requires no external package to load. It is a base R function.
  2. It provides the flexibility to add new rows at any index of the data frame.

Cons

  1. It requires a deeper understanding of row indexing.
  2. It is an error-prone approach because you can make easy mistakes while indexing.
  3. This approach is not suitable for appending rows because its intended use case is different.

Method 4: Using dplyr::add_row() or tidyverse::add_row()

The dplyr::add_row() function intuitively adds rows to a data frame.

library(dplyr)

df <- data.frame(
  name = c("Krunal", "Ankit", "Dhaval"),
  score = c(85, 90, 78),
  subject = c("Math", "Science", "History"),
  class = c(10, 11, 12)
)

# Appending a single row using dplyr::add_row() function
combined_df <- add_row(df,
  name = "Niva",
  score = 99,
  subject = "Physics",
  class = 12
)

print(combined_df)

Output

You can see that the add_row() function allows us to specify the values for each column directly within the function call.

Pros

  1. It offers a clear and concise syntax for adding a single row.

Cons

  1. It requires loading an external package.
  2. It is less efficient for adding multiple rows compared to bind_rows().
  3. It does not work well with complex scenarios.

That’s all!

Recent Posts

R length(): Vector, List, Matrix, Array, Data Frame, String

Before executing an operation on an object, it is advisable to check its length, as…

15 hours ago

How to Round Numbers in R

Rounding is a process of approximating a number to a shorter, simpler, and more interpretable…

2 days ago

Adding Single or Multiple Columns to Data Frame in R

Whether you want to add new data to your existing datasets or create new variables…

4 days ago

sqrt() Function: Calculate Square Root in R

The square root of a number is a value that is multiplied by itself, giving…

5 days ago

How to Remove Duplicate Rows from DataFrame in R

Duplicate rows refer to all the values across all columns that are the same in…

6 days ago

How to Remove NA From Vector in R

A vector is a data structure that holds the same type of data. When working…

1 week ago