It is my understanding that dplyr::bind_rows() only operates on data frames. Why are both of the below identical?
# Load pkgs, set seed
library(dplyr)
set.seed(1)
# Create toy data
foo <- list(df1 = data.frame(A = rnorm(3), B = rnorm(3)),
df2 = data.frame(C = rnorm(3), B = rnorm(3)),
df3 = data.frame(C = rnorm(3), A = rnorm(3)))
df1 <- bind_rows(foo)
# Combine all sources into 1 source
for (i in 1:(length(foo) - 1)){
foo[[i+1]] <- dplyr::bind_rows(foo[[i]], foo[[i+1]])
}
# Extract final df from list
df2 <- foo[[length(foo)]]
# Check for identical
identical(df1, df2)
According to https://rdrr.io/cran/dplyr/man/bind.html
"Each argument can either be a data frame, a list that could be a data frame, or a list of data frames."
bind_rows(foo)
passes a "a list of data frames."
While
bind_rows(foo[[i]], foo[[i+1]])
passes data frames.
Incidentally, you can't pass rbind
a list of data frames, which is why you would have had to use
do.call(rbind, foo)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With