Use input of purrr's map function to create a named list as output in R

后端 未结 3 1302
暗喜
暗喜 2020-12-29 22:28

I am using the map function of the purrr package in R which gives as output a list. Now I would like the output to be a named list based on the input. An example is given be

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-29 23:19

    We just need to name the list

    names(output) <- input
    

    and then extract the elements based on the name

    output$a
    #[1] "test-a"
    

    If this needs to be done using tidyverse

    library(tidyverse)
    output <- map(input, ~paste0('test-', .)) %>% 
                                    setNames(input)
    

提交回复
热议问题