Difference between rbind () and bind_rows () in R

On the Internet, I found that it is used to combine two data frames, and the same task is performed using a function . rbind() bind_rows()

Then I don’t understand what is the difference between these two functions and which is more efficient to use?

+17
source share
2 answers

In addition to a few additional differences, one of the main reasons for using it bind_rows rbindis to combine two data frames with different numbers of columns. rbindit gives an error, while it bind_rowsassigns " NA" to those rows of columns that are not in one of the data frames where the value is not provided by the data frames.

Try the following code to see the difference:

a <- data.frame(a = 1:2, b = 3:4, c = 5:6)
b <- data.frame(a = 7:8, b = 2:3, c = 3:4, d = 8:9)

The results for the two calls are as follows:

rbind(a, b)
> rbind(a, b)
Error in rbind(deparse.level, ...) : 
  numbers of columns of arguments do not match
library(dplyr)
bind_rows(a, b)
> bind_rows(a, b)
  a b c  d
1 1 3 5 NA
2 2 4 6 NA
3 7 2 3  8
4 8 3 4  9
+27
source

bind_rows() , ( NA , ), , rbind().

rbind() , , , . . rbind() . , , bind_rows().

+1

Source: https://habr.com/ru/post/1672670/


All Articles