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

bind_rows() shifts named list identifiers when multiple consecutive NULL elements are present #4296

Closed
koncina opened this issue Mar 20, 2019 · 3 comments · Fixed by #4299
Closed
Assignees
Labels
bug an unexpected problem or unintended behavior
Milestone

Comments

@koncina
Copy link

koncina commented Mar 20, 2019

When row-binding a named list containing multiple consecutive NULL elements the resulting data frame identifiers (using the .id argument) are shifted:

library(tidyverse)

list(a = tibble(expected_id = "a"),
     b = NULL,
     c = NULL,
     d = tibble(expected_id = "d"),
     c = NULL,
     e = tibble(expected_id = "e")) %>% 
  bind_rows(.id = "id")
#> # A tibble: 3 x 2
#>   id    expected_id
#>   <chr> <chr>      
#> 1 a     a          
#> 2 c     d          
#> 3 d     e

Created on 2019-03-20 by the reprex package (v0.2.1)

Single NULL elements are handled accurately:

library(tidyverse)

list(a = tibble(expected_id = "a"),
     b = NULL,
     c = tibble(expected_id = "c"),
     d = NULL,
     e = tibble(expected_id = "e")) %>% 
  bind_rows(.id = "id")
#> # A tibble: 3 x 2
#>   id    expected_id
#>   <chr> <chr>      
#> 1 a     a          
#> 2 c     c          
#> 3 e     e

Created on 2019-03-20 by the reprex package (v0.2.1)

@werkstattcodes
Copy link

just noted that using it with purrr leads to desired result

> list(a = tibble(expected_id = "a"),
+      b = NULL,
+      c = NULL,
+      d = tibble(expected_id = "d"),
+      f = NULL,
+      e = tibble(expected_id = "e")) %>% 
+   map_dfr(., bind_rows, .id="id")
# A tibble: 3 x 2
  id    expected_id
  <chr> <chr>      
1 a     a          
2 d     d          
3 e     e   

@koncina
Copy link
Author

koncina commented Mar 21, 2019

But only because bind_rows() coerces each single NULL element to an empty tibble (just like as_tibble()). map_dfr() per se will also fail to handle indices of multiple NULL elements:

library(tidyverse)

bind_rows(NULL) %>% 
  class()
#> [1] "tbl_df"     "tbl"        "data.frame"

my_list <- list(a = tibble(expected_id = "a"),
     b = NULL,
     c = NULL,
     d = tibble(expected_id = "d"),
     c = NULL,
     e = tibble(expected_id = "e"))

map_dfr(my_list, as_tibble, .id = "id")
#> # A tibble: 3 x 2
#>   id    expected_id
#>   <chr> <chr>      
#> 1 a     a          
#> 2 d     d          
#> 3 e     e

map_dfr(my_list, ~.x, .id = "id")
#> # A tibble: 3 x 2
#>   id    expected_id
#>   <chr> <chr>      
#> 1 a     a          
#> 2 c     d          
#> 3 d     e

Created on 2019-03-21 by the reprex package (v0.2.1)

@romainfrancois romainfrancois added the bug an unexpected problem or unintended behavior label Mar 22, 2019
@romainfrancois romainfrancois added this to the 0.8.1 milestone Mar 22, 2019
@romainfrancois romainfrancois self-assigned this Mar 22, 2019
@lock
Copy link

lock bot commented Sep 18, 2019

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Sep 18, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants