To combine data frames vertically in R, you can use rbind() function or bind_rows() function from dplyr package in R.
The following methods show how you can use it with syntax.
Method 1: Use rbind() Function
rbind(df1,df2,df3,...)
Method 2: Use bind_rows() Function
library(dplyr)
bind_rows(df1,df2,df3,...)
The following example shows how to combine data frames vertically in R.
Combine Data Frame Vertically Using rbind() Function in R
Let’s see how we can use rbind() function in R:
# Create data frame
df1 <- data.frame(Values=c(102,103,101))
df2 <- data.frame(Values=c(100,99,98))
# Combine data frames vertically
df <- rbind(df1,df2)
# Print combined data frame
print(df)
Output:
Values
1 102
2 103
3 101
4 100
5 99
6 98
Here the output shows rows of both data frames are combine vertically using rbind() function.
Combine Data Frames Vertically Using bind_rows() Function in R
Let’s see how we can use bind_rows() function to combine rows in R:
# Load library
library(dplyr)
# Create data frame
df1 <- data.frame(Values=c(102,103,101))
df2 <- data.frame(Values=c(100,99,98))
# Combine data frames vertically
combined_df <- bind_rows(df1, df2)
# Print combined data frame
print(combined_df)
Output:
Values
1 102
2 103
3 101
4 100
5 99
6 98
As the output shows rows are combine from both data frames.