Indrajeet Patil
Indrajeet Patil

Reputation: 4889

getting named lists from purrr::map like in plyr::ldply

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.

Upvotes: 2

Views: 1111

Answers (2)

Tung
Tung

Reputation: 28441

You just need to use purrr::set_names(Species) before doing map

library(plyr)
library(tidyverse)

# creating a list of plots with purrr
plotlist_purrr <- iris %>%
  dplyr::group_by(.data = ., Species) %>%
  tidyr::nest(data = .) %>%
  dplyr::mutate(
    .data = .,
    plots = data %>% 

      purrr::set_names(Species) %>%

      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)

#> [1] "setosa"     "versicolor" "virginica"

Created on 2018-03-22 by the reprex package (v0.2.0).

Upvotes: 1

Roman
Roman

Reputation: 17668

You can try

library(tidyverse)
my_way <- iris %>%
  group_by(Species) %>%
  nest() %>%
  mutate(plots= data %>%  
                  map(~ggplot(., aes(x= Sepal.Length, y= Sepal.Width)) +
                        geom_point() + 
                        geom_smooth(method= "lm"))) %>% 
  mutate(plots= set_names(plots, Species))

my_way
# A tibble: 3 x 3
Species      data              plots   
<fct>        <list>            <list>  
1 setosa     <tibble [50 x 4]> <S3: gg>
2 versicolor <tibble [50 x 4]> <S3: gg>
3 virginica  <tibble [50 x 4]> <S3: gg>

names(my_way$plots)
[1] "setosa"     "versicolor" "virginica" 

Upvotes: 2

Related Questions