purrr:list_rbind() versus dplyr::bind_rows()

I was turned onto Does purrr:list_bind() supersede dplyr::bind_() ? by @phil, which has been closed to answers. I can see where the confusion arises and wanted to provide an easy to understand reprex that shows how purrr:list_rbind() and dplyr::bind_rows() are different to one another (as at purrr 1.0.1 and dplyr 1.1.0).

I would be surprised if the tidyverse teams in question have plans for the former function to supersede the latter because they don't offer equivalent uses.

In the reprex below you can see that

  • dplyr::bind_rows() can take multiple dataframes, lists of dataframes, lists containing named items, lists of lists containing named items, or any combination of these types
  • purrr:list_rbind() can only take a list of dataframes / NULLs
df1 <- data.frame(
  a = c(1, 2, 3), 
  b = c("x", "y", "z"),
  c = c("a h'aon", "a dhà", "a trì")
)

df2 <- data.frame(
  a = c(4, 5, 6), 
  b = c("l", "m", "n"),
  c = c("a ceithir", "a còig", "a sia")
)

list_of_dfs <- list(df1 = df1, 
                    df2 = df2)

list1 <- list(
  a = 1, 
  b = "x",
  c = "a h'aon"
)

list2 <- list(
  a = 4, 
  b = "l",
  c = "a ceithir"
)

list_of_lists <- list(list1 = list1, list2 = list2)

# Dataframes
dplyr::bind_rows(list_of_dfs) # works
dplyr::bind_rows(df1, df2) # also works
dplyr::bind_rows(df1, list2) # even this works!

purrr::list_rbind(list_of_dfs) # works
purrr::list_rbind(df1, df2) # error - "`x` must be a list, not a <data.frame> object."

# Lists
dplyr::bind_rows(list_of_lists) # works
dplyr::bind_rows(list1, list2) # also works

purrr::list_rbind(list_of_lists) # error - "Each element of `x` must be either a data frame or `NULL`."
purrr::list_rbind(list1, list2) # error - same as above
2 Likes

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.