Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Giving the list returned by purrr::map names

Tags:

r

purrr

tidyverse

Is there a way to automatically give names to the returned list given by purrr:map?

For example, I run code like this very often.

fn <- function(x) { paste0(x, "_") }
l <- map(LETTERS, fn)
names(l) <- LETTERS

I'd like for the vector that is being automated upon to automatically become the names of the resulting list.

like image 403
user1775655 Avatar asked Jun 06 '18 17:06

user1775655


1 Answers

We can use imap

imap(setNames(LETTERS, LETTERS), ~ paste0(.x, "_"))

Or map with a named vector

map(setNames(LETTERS, LETTERS), ~ paste0(.x, "_"))
like image 60
akrun Avatar answered Sep 27 '22 23:09

akrun