Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
imap()-在R中的列表中返回结果_R_List_Environment Variables_Apply - Fatal编程技术网

imap()-在R中的列表中返回结果

imap()-在R中的列表中返回结果,r,list,environment-variables,apply,R,List,Environment Variables,Apply,大多数包含的代码都具有可再现性,我的问题是关于从imap()函数导出结果的问题 我已经编写了一些函数来汇总我的数据,如下所示。它创建了一个列表,包含多个列表-每个齿轮对应一个列表 splitCars <- split(mtcars, mtcars$cyl) summarizeMtcarsYearly <- function(x) { #Ngears v1 <- length(unique(x$gear)) v2 <- paste0(unique(levels

大多数包含的代码都具有可再现性,我的问题是关于从
imap()
函数导出结果的问题

我已经编写了一些函数来汇总我的数据,如下所示。它创建了一个列表,包含多个列表-每个齿轮对应一个列表

splitCars <- split(mtcars, mtcars$cyl)

summarizeMtcarsYearly <- function(x)
{
  #Ngears
  v1 <- length(unique(x$gear))
  v2 <- paste0(unique(levels(as.factor(x$gear))),collapse = ', ')
  #Build data
  y <- data.frame(Ngears=v1,gears=v2,stringsAsFactors = F)
  return(y)
}

summarizeMtcars <-function(){

  splitCars <- split(mtcars, mtcars$cyl)
  splitCars <- lapply(splitCars,summarizeMtcarsYearly)

}

splitCars <- summarizeMtcars()
这正是我想要的。但是,现在,我需要返回列表中每个档位的所有表格,如下所示:

createSummaryTable <- function(x, y){

  tab <- ... # this is the same as before


  tabname <- paste0("summary_", y)
  assign(tabname, tab)

}

analysis.summaryTables <- function(){
  
  # create tables
    splitCars <- summarizeMtcars()
    imap(splitCars, function(x, y) createSummaryTable(x,y))
  
  # append all tables to one list
  tables <- ls(patter = "summary_")
  out <- do.call(c,list(tables))
  
  
}

createSummaryTable如果我理解正确,您有一个函数
createSummaryTable
可以创建一个对象,一个特定的表

您有一个命名数据帧列表,并且希望将此列表映射到函数中,以返回一个对象列表(具体为一个表列表),其中的对象名称相同,但“summary_”必须出现在前面

因此:

createSummaryTable <- function(x, y){

  # do something here

  return(tbl)

}

# map your list
out <- purrr::imap(list_of_named_dataframes, createSummaryTable)
names(out) <- paste("summary", names(out), sep = "_")

imap
中的
createSummaryTable第二个参数是名称的字符串。如果我从你的代码中理解正确,那么你使用的是反向的。我很震惊,我不明白你的意思@Edo。请您详细解释一下好吗?
tabname哦,是的,当然,您是绝对正确的您的示例非常长,只需在
imap
上提问即可。你确定不能缩短它吗?我确实需要imap函数,因为在
createSummaryTable
中,我使用索引来命名每个表,我更改了答案。但是我仍然相信你应该用
names单独编辑列表的名称。结果对你有用吗?我已经实现了它,但是我得到了一个由3个元素组成的字符向量。。。如果要将最后两行放入包装函数中,则通过省略lat行
名称(out),需要在末尾写入
out
return(out)
。否则,您将获得
names(out)
作为输出,因为这是不可见返回的最后一个元素。小心这类东西。
createSummaryTable <- function(x, y){

  tab <- ... # this is the same as before


  tabname <- paste0("summary_", y)
  assign(tabname, tab)

}

analysis.summaryTables <- function(){
  
  # create tables
    splitCars <- summarizeMtcars()
    imap(splitCars, function(x, y) createSummaryTable(x,y))
  
  # append all tables to one list
  tables <- ls(patter = "summary_")
  out <- do.call(c,list(tables))
  
  
}
summaryTables <- analysis.summaryTables()
createSummaryTable <- function(x, y){

  # do something here

  return(tbl)

}

# map your list
out <- purrr::imap(list_of_named_dataframes, createSummaryTable)
names(out) <- paste("summary", names(out), sep = "_")