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 first call to bind_rows
(test1
) generates the error
Error in bind_rows_(x, .id) : Argument 1 must have names
Using do.call
to invoke bind_rows
(test2
), on the other hand, works as expected:
test2
# A tibble: 2 x 1
a
<int>
1 1
2 2
Why? This is using dplyr 0.7.6 and purrr 0.2.5. If I use map_df
instead of map
, it fails with the same error.
Note: It doesn't appear to me that this question is the same as Error in bind_rows_(x, .id) : Argument 1 must have names using map_df in purrr.
EDIT: The other way to address this issue is by explicitly creating a dataframe in the first place:
y <- purrr::map(1:2, ~ data.frame(a=.x))
test1
and test2
are now created with no errors and are identical.
Alternatively,this creates the test2
data frame in one step:
purrr::map_df(1:2, ~ data.frame(a=.x))
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…