I understand how use named vectors with purrr, as in this example:
library(tidyverse)
plain_vector <- c(2, 3)
named_vector <- set_names(plain_vector, plain_vector)
map_df(named_vector, ~ sample_n(mtcars[, 1:2], size = .), .id = "size")
#> size mpg cyl
#> 1 2 17.8 6
#> 2 2 19.7 6
#> 3 3 26.0 4
#> 4 3 13.3 8
#> 5 3 21.4 4
Created on 2019-10-04 by the reprex package (v0.3.0)
But that seems a bit of a hack. I often just have a simple unnamed vector, as in plain_vector above. How can I ensure that the values in the input vector get transferred to the resulting object, even in situations in which the vector has no names? The obvious approach gives me:
library(tidyverse)
plain_vector <- c(2, 3)
map_df(plain_vector, ~ sample_n(mtcars[, 1:2], size = .), .id = "size")
#> size mpg cyl
#> 1 1 10.4 8
#> 2 1 15.2 8
#> 3 2 13.3 8
#> 4 2 15.2 8
#> 5 2 15.5 8
Created on 2019-10-04 by the reprex package (v0.3.0)
Note that, instead of keeping the values of size --- 2 and 3 --- I get the index of their position in the plain_vector input --- 1 and 2. This is not a bug. The documentation for .id reports:
Either a string or
NULL
. If a string, the output will contain a variable with that name, storing either the name (if.x
is named) or the index (if.x
is unnamed) of the input.
But it still seems like there should be a better way than just giving every vector names which correspond to the values of that vector. Am I missing something obvious?