Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: dplyr::bind_rows() operating on lists?

Tags:

r

dplyr

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)
like image 516
user3614648 Avatar asked Jun 26 '17 16:06

user3614648


1 Answers

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)
like image 131
Jeremy Voisey Avatar answered Oct 13 '22 08:10

Jeremy Voisey