1

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 second b 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

cephalopod
  • 1,826
  • 22
  • 31

2 Answers2

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
Prem
  • 11,775
  • 1
  • 19
  • 33
  • I wanted to use future_map2 for this. It speeds-up my read times, from 31 mins to 6 mins! – cephalopod May 05 '18 at 21:56
  • Was this benchmarking done between `mapply` & `furrr`? btw we are happy to know that you found a solution using `names(res) <- purrr::map_chr(res, ~attr(.x, "name"))`. – Prem May 06 '18 at 03:19
  • I have not done the benchmarking. I never learned `mapply`, I concentrated my efforts in `purrr`, I could not understand the `apply` group of function despite my best efforts :( – cephalopod May 06 '18 at 04:16
0

@DavisVaughan provided the solution https://github.com/DavisVaughan/furrr/issues/10

cephalopod
  • 1,826
  • 22
  • 31