.id parameter to purrr::map2_dfr doesn't avoid "Argument 1 must have names" error

I thought the idea of the .id parameter to map2_dfr was supposed to provide a variable name:

the output will contain a variable with that name

but that doesn't work with my example (reprex below):


lows <- 1:7 %>%
  `*`(10) %>%
highs <- rep(9, 6) %>%
  c(., 15) %>%
  map2_dbl(lows, `+`)

# I want this vector, but as a 1-column tibble:
purrr::map2_chr(lows, highs, ~ paste0(.x, "-", .y))
#> [1] "15-24" "25-34" "35-44" "45-54" "55-64" "65-74" "75-90"
purrr::map2_dfr(lows, highs, ~ paste0(.x, "-", .y), .id = "age_group")
#> Error: Argument 1 must have names.

Created on 2021-06-21 by the reprex package (v2.0.0)

I could add an extra line and use tibble::enframe or similar to get the result I want, but I would like to understand what is wrong with the more compact map2_dfr approach I'm trying to use. Why is the .id parameter not providing a column name and preventing the error?

The .id argument is specifically for when the argument to map is a named list or vector. Your arguments don't have names which is why you're seeing the error.

c("dave" = 1, "adrian" = 2, "bruce" = 3, "steve" = 4) %>% map_dfr(~tibble(num = .), .id = "member")

Oh, I understand now. The .id is not to provide a variable name for the values themselves, but to create an id variable in the output alongside the values.
I didn't understand the documentation clearly.

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.