pmap_df报bind_rows参数无名称错误,map_df能否替代plyr::ldply?
Argument 1 must have names Error & purrr vs. plyr::ldply Great question—this exact issue used to drive me crazy too when I first switched from plyr to purrr. Let’s break down both the fix for your error and whether map_df can truly replace ldply.
Fixing the bind_rows Naming Error
The core problem here is that map_df/pmap_df rely on dplyr::bind_rows under the hood, which requires named inputs when passing vectors or non-dataframe list elements. But there’s a much cleaner workaround now, thanks to updates in the purrr package:
Use list_rbind() instead of *_df functions
Instead of using pmap_df() directly, run your pmap() call first, then pipe the result to purrr::list_rbind():
# Instead of this (which throws an error): pmap_df(my_inputs, my_function) # Do this instead: pmap(my_inputs, my_function) %>% list_rbind()
list_rbind() is built to handle both named and unnamed lists gracefully—no need to tack on arbitrary names to your outputs just to satisfy bind_rows. If you want to add an ID column tracking which list element came from which input (like ldply’s .id argument), you can do that easily:
pmap(my_inputs, my_function) %>% list_rbind(.id = "source_id")
Temporary workaround (for older purrr versions)
If you’re stuck with an older purrr release that doesn’t have list_rbind(), you can manually convert each function output to a single-row dataframe inside your function. For example:
my_function <- function(a, b, c) { result <- c(a + b, c * 2) # Wrap the result in a dataframe to avoid naming issues tibble::tibble(col1 = result[1], col2 = result[2]) } pmap_df(my_inputs, my_function)
This bypasses the error because you’re passing dataframes (which have explicit column names) to bind_rows.
Can map_df Series Fully Replace plyr::ldply?
Short answer: Yes, now that purrr has list_rbind() and list_cbind()—but early versions of purrr had gaps (like this naming requirement) that made ldply still useful for edge cases.
ldplywas always flexible with unnamed lists—it would automatically coerce vectors into dataframe rows without fuss. Earlymap_dfcouldn’t match this because ofbind_rows’ restrictions.- Modern purrr’s
list_rbind()matchesldply’s flexibility, while integrating better with the tidyverse ecosystem (like working seamlessly with tibbles, supporting.idfor source tracking, and handling more edge cases).
So unless you’re stuck on a very old purrr version, you don’t need to keep relying on ldply anymore—map() + list_rbind() is the clean, tidyverse-native replacement.
内容的提问来源于stack exchange,提问作者Novus

