Error in bind_rows_(x, .id) : Argument 1 must have names

后端 未结 1 1567
暖寄归人
暖寄归人 2021-01-07 22:02

Here is a code snippet:

y <- purrr::map(1:2, ~ c(a=.x))
test1 <- dplyr::bind_rows(y)
test2 <- do.call(dplyr::bind_rows, y)

The fir

相关标签:
1条回答
  • 2021-01-07 22:34

    From the documentation of bind_rows:

    Note that for historical reasons, lists containg vectors are always treated as data frames. Thus their vectors are treated as columns rather than rows, and their inner names are ignored

    Here, your y as constructed has only inner names - it is two unnamed list elements, each containing a length-one vector with the vector element named a. So this error seems to be expected.

    If you name the list elements, you can see that it behaves as described, with the vectors treated as columns:

    library(tidyverse)
    y <- map(1:2, ~ c(a=.x)) %>%
      set_names(c("a", "b"))
    bind_rows(y)
    #> # A tibble: 1 x 2
    #>       a     b
    #>   <int> <int>
    #> 1     1     2
    

    The difference with supplying y as arguments via do.call is that it's more like writing bind_rows(c(a = 1), c(a = 2)). This is not a list containing vectors, but separate vectors, so it binds by row as expected.

    0 讨论(0)
提交回复
热议问题