How to name a dataframe so that I can look for it within a list

偶尔善良 提交于 2019-12-23 01:08:09

问题


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


回答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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!