bind_rows fails when column names are numbers

Let's say I have a list named testL

testL <- 
list(s = structure(c(`21` = 1590L, `22` = 1417L, `23` = 505L, 
`24` = 137L, `41` = 32L, `42` = 14L, `43` = 18L, `90` = 8L), .Dim = 8L, .Dimnames = structure(list(
    c("21", "22", "23", "24", "41", "42", "43", "90")), .Names = ""), class = "table"), 
    t = structure(c(`21` = 192L, `22` = 800L, `23` = 437L, `24` = 123L
    ), .Dim = 4L, .Dimnames = structure(list(c("21", "22", "23", 
    "24")), .Names = ""), class = "table"))

bind_rows fails while rbind_all works fine.


bind_rows(testL)
Error: Argument 2 must be length 8, not 4

rbind_all(testL)
# A tibble: 2 x 8
   `21`  `22`  `23`  `24`  `41`  `42`  `43`  `90`
  <int> <int> <int> <int> <int> <int> <int> <int>
1  1590  1417   505   137    32    14    18     8
2   192   800   437   123    NA    NA    NA    NA
Warning message:
'rbind_all' is deprecated.
Use 'bind_rows()' instead.
See help("Deprecated")

Is this expected? I can't find documentation to sort out this problem when in fact rbind_all is no longer in the package.

Edited : :slight_smile: I think I rushed to answer. I need to go slower

EDIT 2 :
this issue is because bind_rows wants to work on dataframes as inputs , not plain lists... so the approach I show below wraps up the lists as one row dataframes that an be bind_rows()'ed together
The first way is explicit, the second implicit, using purrr packages map_dfr()

testL <- 
  list(s = structure(c(`21` = 1590L, `22` = 1417L, `23` = 505L, 
                       `24` = 137L, `41` = 32L, `42` = 14L, `43` = 18L, `90` = 8L), .Dim = 8L, .Dimnames = structure(list(
                         c("21", "22", "23", "24", "41", "42", "43", "90")), .Names = ""), class = "table"), 
       t = structure(c(`21` = 192L, `22` = 800L, `23` = 437L, `24` = 123L
       ), .Dim = 4L, .Dimnames = structure(list(c("21", "22", "23", 
                                                  "24")), .Names = ""), class = "table"))


testL2 <- list( enframe(testL$s) %>% 
                  pivot_wider(names_from = name,values_from = value),
                enframe(testL$t) %>% 
                  pivot_wider(names_from = name,values_from = value))

#check result
bind_rows(testL2)

## more elegantly ?
list_to_row_frame <- function(l) {
  enframe(l) %>%
    pivot_wider(names_from = name, values_from = value)
}
#library(purrr) # map_dfr is in purrr namespace
#another way
testL3 <- purrr::map_dfr(testL,~list_to_row_frame(.))
1 Like

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