How can I get a named list back from purrr::map
the same way I could do it with plyr::dlply
? I am providing a reprex here. As can be seen, plyr::ldply returns a named list while purrr::map
doesn't. I also checked out a similar question from 2 years ago (How to get list name and slice name with pipe and purrr), but that wasn't that helpful because there purrr::map
is not being used on a list column inside a dataframe and that's what I want to do.
library(tidyverse)
library(plyr)
# creating a list of plots with purrr
plotlist_purrr <- iris %>%
dplyr::group_by(.data = ., Species) %>%
tidyr::nest(data = .) %>%
dplyr::mutate(
.data = .,
plots = data %>% purrr::map(
.x = .,
.f = ~ ggplot2::ggplot(
data = .,
mapping = aes(x = Sepal.Length, y = Sepal.Width)
) + geom_point() + geom_smooth(method = "lm")
)
)
# see the names of the plots
names(plotlist_purrr$plots)
#> NULL
# creating a list of plots with plyr
plotlist_plyr <- plyr::dlply(
.data = iris,
.variables = .(Species),
.fun = function(data)
ggplot2::ggplot(
data = data,
mapping = aes(x = Sepal.Length, y = Sepal.Width)
) + geom_point() + geom_smooth(method = "lm")
)
# see the names of the plots
names(plotlist_plyr)
#> [1] "setosa" "versicolor" "virginica"
Created on 2018-03-22 by the reprex package (v0.2.0).
I am trying to move away from plyr
and completely depend on tidyverse
in my scripts but some of the things I could do with plyr
I am still trying to figure out how to them with purrr
and this is one of them.