【发布时间】:2018-10-05 04:54:14
【问题描述】:
我想迭代一个命名列表(带有地图),但不知何故,单个列表的工作方式无法大规模工作。这里有什么问题,我必须改变什么才能让它工作?
我怀疑它有什么。与 list[1] 和 list[[1]] 之间的区别有关,但我在 atm 错过了它。
library(rlang)
library(tidyverse)
# this works
single_list <- list(one = 1)
create_function <- function(mylist){
function(){
x <- names(mylist)
n <- purrr::flatten_chr(mylist)
rep(x, n)
}
}
one <- create_function(single_list)
one()
#> [1] "one"
# this doesn't work
long_list <- list(one = 1,
two = 2,
three = 3)
fun <- long_list %>%
map(create_function)
fun$one()
#> Error: `.x` must be a list (double)
【问题讨论】:
-
您需要
list或lists 即map(long_list, ~ create_function(list(.x))() )