我正在尝试对同一数据多次调用同一函数,但函数参数不同。我的问题可以描述为:
x <- as.character(1:5)
l <- list(list(name = "a", collapse = ""), list(name = "b", collapse = "-"))
output <- list()
for(l_cur in l) {
output[[l_cur$name]] <- x %>% paste(collapse = l_cur$collapse)
}
是否有使用purrr
的方法更简洁?(背景:我想将其与rvest
一起使用,因为我多次在同一数据上调用html_nodes()
,但只更改了css
参数。)
可能您可以使用:
library(purrr)
map(l, ~x %>% paste(collapse = .x$collapse)) %>% set_names(map(l, pluck, "name"))
#$a
#[1] "12345"
#$b
#[1] "1-2-3-4-5"