Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.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
在tidyverse中命名迭代映射的列表_R_Reshape2_Purrr_Tidyverse - Fatal编程技术网

在tidyverse中命名迭代映射的列表

在tidyverse中命名迭代映射的列表,r,reshape2,purrr,tidyverse,R,Reshape2,Purrr,Tidyverse,我经常在循环中进行循环,然后最后将列表融化到数据框中进行绘图 ac<-list("BB","AA") ca<-list(a=c(1,2,3),b=c(6,5,4)) cc<-map(a,function(y) map(ca,~paste0(.x,y))) reshape2::melt(cc) 结果df的名称为L2和L1,如果名称分别为ca和ac,则我更喜欢 nam.cons<-NULL map_test2<-function(list_in,...){ nam

我经常在循环中进行循环,然后最后将列表融化到数据框中进行绘图

ac<-list("BB","AA")
ca<-list(a=c(1,2,3),b=c(6,5,4))
cc<-map(a,function(y) map(ca,~paste0(.x,y)))
reshape2::melt(cc)
结果df的名称为L2和L1,如果名称分别为ca和ac,则我更喜欢

nam.cons<-NULL
map_test2<-function(list_in,...){
  nam.cons<<-c(nam.cons,deparse(substitute(list_in)))

  if (is.null(names(list_in))){
    names(list_in)<-list_in
  }
  map(list_in,...)
}

cc3=map_test2(ac,function(y) map_test2(ca,~paste0(.x,y)))
cc4<-reshape2::melt(cc3)
names(cc4)<-c("value",rev(unique(nam.cons)))

nam.cons我们希望在不丢失列表名称的情况下,优雅地遍历列表项的每个组合

因此,让我们构建所有组合并存储名称:

library(tidyverse)               # data
ac<-list("BB","AA")              #
ca<-list(a=c(1,2,3),b=c(6,5,4))  #

df <- merge(tibble(ac),tibble(ca)) 

#   ac      ca
# 1 BB 1, 2, 3
# 2 AA 1, 2, 3
# 3 BB 6, 5, 4
# 4 AA 6, 5, 4
library(tidyverse)               # data
ac<-list("BB","AA")              #
ca<-list(a=c(1,2,3),b=c(6,5,4))  #

df <- merge(tibble(ac),tibble(ca)) 

#   ac      ca
# 1 BB 1, 2, 3
# 2 AA 1, 2, 3
# 3 BB 6, 5, 4
# 4 AA 6, 5, 4
df %>%
  mutate(ac=unlist(ac),
         name =names(ca)) %>%
  unnest %>%
  mutate(value = paste0(ca,ac))

#    ac name ca value
# 1  BB    a  1   1BB
# 2  BB    a  2   2BB
# 3  BB    a  3   3BB
# 4  AA    a  1   1AA
# 5  AA    a  2   2AA
# 6  AA    a  3   3AA
# 7  BB    b  6   6BB
# 8  BB    b  5   5BB
# 9  BB    b  4   4BB
# 10 AA    b  6   6AA
# 11 AA    b  5   5AA
# 12 AA    b  4   4AA