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:
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.
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 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.
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.
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
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
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.
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!
Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.
Before executing an operation on an object, it is advisable to check its length, as…
Rounding is a process of approximating a number to a shorter, simpler, and more interpretable…
Whether you want to add new data to your existing datasets or create new variables…
The square root of a number is a value that is multiplied by itself, giving…
Duplicate rows refer to all the values across all columns that are the same in…
A vector is a data structure that holds the same type of data. When working…