使用purrr的map函数的输入创建一个命名列表作为R中的输出 [英] Use input of purrr's map function to create a named list as output in R
本文介绍了使用purrr的map函数的输入创建一个命名列表作为R中的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在R中使用purrr包的map函数,该函数给出输出列表.现在,我希望输出是基于输入的命名列表.下面是一个示例.
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 below.
input <- c("a", "b", "c")
output <- purrr::map(input, function(x) {paste0("test-", x)})
由此,我想使用以下方式访问列表的元素:
From this I would like to access elements of the list using:
output$a
或
output$b
推荐答案
我们只需要命名list
names(output) <- input
,然后根据名称提取元素
and then extract the elements based on the name
output$a
#[1] "test-a"
如果需要使用tidyverse
library(tidyverse)
output <- map(input, ~paste0('test-', .)) %>%
setNames(input)
这篇关于使用purrr的map函数的输入创建一个命名列表作为R中的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文