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
Assignees
Labels
Milestone

Comments

@koncina
Copy link

@koncina 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

@werkstattcodes werkstattcodes commented Mar 20, 2019

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 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)

@lock
Copy link

@lock 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
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

3 participants