`flatten_dfr` throws an error that doesn't make sense to me

Example

list(tbl1 = tibble(a = 1:3, b = 11:13), 
     tbl2 = tibble(a = 4:6, b = 14:16)) %>% flatten_dfr
#Error: Column names `a` and `b` must not be duplicated.

The error indicates the column names of different tibbles should not be duplicated, but I don't understand why. Am I doing something wrong?

I also think this is not supposed to happen, maybe you stumbled upon a bug. I checked to with map_dfr, but there is no issue there, so this is not intended behaviour of the *_dfr verbs.

library(tidyverse)

list(
  tbl1 = tibble(a = 1:3, b = 11:13), 
  tbl2 = tibble(a = 4:6, b = 14:16)
) %>% 
  flatten_dfr()
#> Error: Column names `a` and `b` must not be duplicated.

list(
  tbl1 = tibble(a = 1:3, b = 11:13), 
  tbl2 = tibble(a = 4:6, b = 14:16)
) %>% 
  map_dfr(~ .x)
#> # A tibble: 6 x 2
#>       a     b
#>   <int> <int>
#> 1     1    11
#> 2     2    12
#> 3     3    13
#> 4     4    14
#> 5     5    15
#> 6     6    16

However, if you want to do your job I would suggest bind_rows in these situations.

list(
  tbl1 = tibble(a = 1:3, b = 11:13), 
  tbl2 = tibble(a = 4:6, b = 14:16)
) %>% 
  bind_rows()
#> # A tibble: 6 x 2
#>       a     b
#>   <int> <int>
#> 1     1    11
#> 2     2    12
#> 3     3    13
#> 4     4    14
#> 5     5    15
#> 6     6    16

If I were you, I would wait a little bit in case anyone else provides a more elaborate answer and I would open an issue in purrr.

1 Like
library(tidyverse)

list(
  tbl1 = tibble(a = 1:3, b = 11:13), 
  tbl2 = tibble(a = 4:6, b = 14:16)
)-> l1  

# what you would want
bind_rows(l1)
# not the use case...
flatten_dfr(l1)

# the use case
flatten_dfr(list(l1))

#because bind_rows doesnt handle this nicely
bind_rows(list(l1))

#unless you pick it out of the list with possibly ugly looking syntax
bind_rows(list(l1)[[1]])
1 Like

This topic was automatically closed 7 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.