R Advanced

Appending Single and Multiple Rows to a Data Frame in R

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 updated to include 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.

Method 2: Using rbind()

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

Appending a single row

The rbind() function works similarly to 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, binding both data frames row-wise and returning 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 combines both data frames row-wise and returns 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

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 to 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

As shown in the above figure, we utilize the nrow() function to determine the length of the data frame and add + 1 (the last index) to append a new row at the end of the data frame. 

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

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.

That’s all!

Recent Posts

cbind() Function: Binding R Objects by Columns

R cbind (column bind) is a function that combines specified vectors, matrices, or data frames…

2 weeks ago

rbind() Function: Binding Rows in R

The rbind() function combines R objects, such as vectors, matrices, or data frames, by rows.…

2 weeks ago

as.numeric(): Converting to Numeric Values in R

The as.numeric() function in R converts valid non-numeric data into numeric data. What do I…

3 weeks ago

Calculating Natural Log using log() Function in R

The log() function calculates the natural logarithm (base e) of a numeric vector. By default,…

4 weeks ago

Dollar Sign ($ Operator) in R

In R, you can use the dollar sign ($ operator)  to access elements (columns) of…

1 month ago

Calculating Absolute Value using abs() Function in R

The abs() function calculates the absolute value of a numeric input, returning a non-negative (only…

2 months ago