Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FR: recursive option for list_rbind() #1055

Closed
DanChaltiel opened this issue Feb 11, 2023 · 1 comment
Closed

FR: recursive option for list_rbind() #1055

DanChaltiel opened this issue Feb 11, 2023 · 1 comment

Comments

@DanChaltiel
Copy link

Hi,

Could it be considered to make list_rbind() recursive?

I often encounter nested lists that need to be "dataframed", for instance:

library(tidyverse)
x = list(
  a = list(a1=sample_n(iris, 1), a2=sample_n(iris, 1)),
  b = list(b1=sample_n(iris, 1), b2=sample_n(iris, 1))
)

list_rbind(x)
#> Error in `list_rbind()`:
#> ! Each element of `x` must be either a data frame or `NULL`.
#> i Elements 1 and 2 are not.

In this simple case, this can be dealt with using map(), although it is less elegant:

x %>% map(list_rbind) %>% list_rbind()
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
#> 1          5.8         2.8          5.1         2.4  virginica
#> 2          6.9         3.1          5.4         2.1  virginica
#> 3          4.4         3.2          1.3         0.2     setosa
#> 4          5.1         2.5          3.0         1.1 versicolor

However, in more complicated cases, it can get more complex to get a single dataframe in the end:

x2 = list(
  a = sample_n(iris, 1),
  b = list(b1=sample_n(iris, 1), b2=sample_n(iris, 1))
)
x2 %>% map(list_rbind) %>% list_rbind()
#> Error in `map()`:
#> i In index: 1.
#> i With name: a.
#> Caused by error in `.f()`:
#> ! `x` must be a list, not a <data.frame> object.

Also, in more deep nested lists, mapping several times can get tedious and error-prone.

Is this something that would belong in purrr?

Maybe with a recursive=TRUE option?

@hadley
Copy link
Member

hadley commented Jul 26, 2023

Definitely out of scope, sorry. Our experience is that doing to do this sort of thing 100% automatically inevitably leads to weird edge cases.

@hadley hadley closed this as completed Jul 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants