What is the Rbind function in R?
rbind() is a function in R used to combine multiple data frames or matrices by row-binding them together. This function appends the rows of the second (and subsequent) data frame(s) or matrix/matrices to the first data frame or matrix.
rbind(..., deparse.level = 1)
- …: Represents the data frames or matrices to be combined. You can provide multiple arguments separated by commas.
- deparse.level: Controls the level of deparsing to be done.
Let’s say you have two data frames df1 and df2, and you want to combine them row-wise using rbind():
# Create sample data frames
df1 <- data.frame(ID = 1:3, Name = c("Jack", "Albert", "Boby"))
df2 <- data.frame(ID = 4:6, Name = c("Emma", "Michael", "Sophia"))
df1
df2
# Combine data frames using rbind
combined_df <- rbind(df1, df2)
# Print the combined data frame
print(combined_df)
Output:
df1 ID Name
1 1 Jack
2 2 Albert
3 3 Boby df2 ID Name 1 4 Emma 2 5 Michael 3 6 Sophia Print the combined data frame ID Name
1 1 Jack
2 2 Albert
3 3 Boby 4 4 Emma 5 5 Michael 6 6 Sophia
In this example, rbind() combines the rows of df1 and df2, resulting in a new data frame combined_df with all the rows from both df1 and df2.
You can also use rbind() to combine matrices in a similar way:
# Create sample matrices
mat1 <- matrix(1:6, nrow = 2)
mat2 <- matrix(7:12, nrow = 2)
mat1
mat2
# Combine matrices using rbind
combined_mat <- rbind(mat1, mat2)
# Print the combined matrix
print(combined_mat)
Output:
mat1
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
mat2
[,1] [,2] [,3]
[1,] 7 9 11
[2,] 8 10 12
Print the combined matrix
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
[3,] 7 9 11
[4,] 8 10 12
Here, rbind() combines the rows of mat1 and mat2, resulting in a new matrix combined_mat with all the rows from both matrices.
Rbind In R
In this article, we will discuss what is Rbind function and how it works in the R Programming Language.
Contact Us