The rbind() function combines R objects, such as vectors, matrices, or data frames, by rows. It stacks rows vertically.
The above figure illustrates how the rbind() method works on data frames, combining their rows.
The data frame that is being stacked must have the same number of columns (or elements, in the case of vectors), and the columns should be of compatible types.
rbind(..., deparse.level = 1)
Argument | Description |
… | It represents data frames, matrices, or vectors that need to be combined. For example, if we want to combine two data frames, they can be df1 and df2. |
deparse.level | It controls how row names are constructed when combining objects.
|
Let’s define two data frames, df1 and df2, with the same number of columns. Both data frames contain three rows, and after combining, the output data frame must have six rows.
# Create two data frames
df1 <- data.frame(col1 = c(1, 2, 3), col2 = c(11, 19, 21))
df2 <- data.frame(col1 = c(4, 5, 6), col2 = c(10, 46, 18))
# Row-bind the two data frames
df_new <- rbind(df1, df2)
# View the updated data frame
print(df_new)
Output
When data frames have different columns, this function will throw an error.
For example, let’s take a df1 with A and B columns and a df2 with A and C columns.
# Data frames with different columns
df1 <- data.frame(A = 1:2, B = c("x", "y"))
df2 <- data.frame(A = 3:4, C = c("z", "w"))
# Combine by rows
df_with_different_cols <- rbind(df1, df2)
print(df_with_different_cols)
# Output:
# Error in match.names(clabs, names(xi)) :
# names do not match previous names
# Calls: rbind -> rbind -> match.names
# Execution halted
Since the column names are not the same, we got an Error in match.names(clabs, names(xi)).
If the number of columns is also different, it will throw the same error.
df1 <- data.frame(col1 = c(1, 2, 3), col2 = c(11, 19, 21))
df2 <- data.frame(col1 = c(10, 18, 46))
# Append the vectors as new rows to the data frame
df_new <- rbind(df1, df2)
# View the updated data frame
print(df_new)
Output
Error in rbind(deparse.level, ...) :
numbers of columns of arguments do not match
Calls: rbind -> rbind
Execution halted
We encountered the error because df1 has two columns and df2 has only one column.
Ensure that both data frames have the same columns (number of columns and the names of columns) to stack them vertically.
You can also use the dplyr::bind_rows() method for data frames, as it handles column mismatches gracefully.
Use deparse.level argument when combining unnamed vectors, not data frames, because with data frames, row names are controlled by row.names() or default numeric values.
v1 <- c(11, 2)
v2 <- c(3, 41)
# Combine with different deparse.level values
r0 <- rbind(v1, v2, deparse.level = 0)
r1 <- rbind(v1, v2, deparse.level = 1)
r2 <- rbind(v1, v2, deparse.level = 2)
rownames(r0)
# Output: NULL
rownames(r1)
# Output: [1] "v1" "v2"
rownames(r2)
# Output: [1] "v1" "v2"
You can see that the rbind() method returns NULL for rownames() when the deparse.level = 0 argument is used.
However, it returns deparsed row names like “v1” and “v2” for both deparse.level = 1 and 2 when passed as symbols (not evaluated or unnamed vectors).
What if we have one data frame and a vector? How do we combine it efficiently? Well, rbind() can efficiently append the vector values as a row to the existing data frame.
See the figure below:
# Create a data frame
df <- data.frame(col1 = c(1, 2, 3), col2 = c(11, 19, 21))
# Create a vector
new_row <- c(4, 5)
# Append the vector as a new row to the data frame
df_new <- rbind(df, new_row)
# View the updated data frame
df_new
Output
The above output screenshot shows that the vector values have been appended to the fourth row of the data frame.
We can also append multiple vectors to the data frame if the vectors contain the same number of elements as the rows of the data frame.
For example, if the df1 row has two elements, the vector we want to combine should contain two elements as well.
df <- data.frame(col1 = c(1, 2, 3), col2 = c(11, 19, 21))
# Create vectors
new_row1 <- c(4, 5)
new_row2 <- c(6, 7)
# Append the vectors as new rows to the data frame
df_new <- rbind(df, new_row1, new_row2)
# View the updated data frame
print(df_new)
Output
Let’s use the built-in data set mtcars for this example.
df_new <- rbind(mtcars, mtcars)
# View the updated data frame
print(df_new)
Output
We can bind multiple vectors to create a new matrix using the rbind() method.
#create two vectors
a <- c(11, 21, 19, 46, 18)
b <- c(1, 2, 3, 4, 5)
#rbind the two vectors into a matrix
new_matrix <- rbind(a, b)
#view matrix
print(new_matrix)
Output
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.
R cbind (column bind) is a function that combines specified vectors, matrices, or data frames…
The as.numeric() function in R converts valid non-numeric data into numeric data. What do I…
The log() function calculates the natural logarithm (base e) of a numeric vector. By default,…
In R, you can use the dollar sign ($ operator) to access elements (columns) of…
The abs() function calculates the absolute value of a numeric input, returning a non-negative (only…
When working with R in an interactive mode, you don't need to use any functions…