I'm trying to run purrr map
across vector inputs, and in the output i'd like the output columns to have meaningful names.
x <- c("a", "b")
y <- "end."
map_dfc(x, function(x) paste("pre ", x, y))
names(x) <- x
map_dfc(x, function(x) paste(x, y))
This is the output I expect, which has column names a
and b
:
# A tibble: 1 x 2
# a b
# <chr> <chr>
# pre a end. pre b end.
Is there a way to avoid the need to run
names(x) <- x
i.e.
x <- c("a", "b")
y <- "end."
map_dfc(x, function(x) paste("pre ", x, y))
# A tibble: 1 x 2
# a b
# <chr> <chr>
yields the data.frame/tibble with column names already attached?
I use map alot and often forget if the input vector has names or not.
set_names
?set_names(map_dfc(x, function(x) paste("pre ", x, y)), x)
? - Ronak Shah