rguides

How to Append Rows to Data Frames in R

Append rows to a data frame when you are building datasets programmatically or collecting output from a loop. The simplest approach is dplyr::bind_rows(), which matches columns by name and fills mismatched columns with NA. If you are working interactively and just need to tack on a single observation, rbind() gets the job done with slightly less ceremony. For data.table users, rbindlist() is the fastest path to row-binding multiple tables at once.

library(dplyr)

df <- data.frame(name = c("Alice", "Bob"), age = c(25, 30))
new_row <- data.frame(name = "Charlie", age = 28)
df <- bind_rows(df, new_row)

Base R’s rbind() works too but requires identical column names and types in both data frames. For data.table, pair rbind() with a list():

library(data.table)
dt <- data.table(name = c("Alice", "Bob"), age = c(25, 30))
dt <- rbind(dt, list(name = "Charlie", age = 28))

Adding rows one at a time inside a loop is slow because R copies the entire data frame on each iteration. Pre-allocate columns upfront with vector("numeric", n) and fill by index, or collect rows in a list and call bind_rows() once at the end.

See also