问题
I have a function that returns a dataframe. I use this function with furrr::future_map2
so that I get a list with several dataframes.
- What I want is the ability to use the
name
input in the function to name the dataframe so that I can search the return list by name.
example
test <- function(x, name){
require(tidyverse)
z <- data.frame(x+1) %>% stats::setNames(., "a")
return(z)
}
furrr::future_map2(1:3, c("a", "b", "c"), ~test(.x, .y))
- The first df within the list would be
a
, the secondb
and so on - The naming should be done within the function
- The option of
names(list.return) <- vector.of.list.names.in.character
does NOT work for me.
Please help
回答1:
How about this?
mapply(
function(x,y){
data.frame(y+1) %>% setNames(., x)
},
c("a", "b", "c"), 1:3, USE.NAMES = T, SIMPLIFY = F)
Output is:
$a
a
1 2
$b
b
1 3
$c
c
1 4
回答2:
@DavisVaughan provided the solution https://github.com/DavisVaughan/furrr/issues/10
来源:https://stackoverflow.com/questions/50187445/how-to-name-a-dataframe-so-that-i-can-look-for-it-within-a-list