2
我經常在循環內部執行循環,然後最終將列表融合到數據框中進行繪圖。命名融合的迭代映射列表在遍歷中
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)
Q1:是否有這樣做的地圖內的地圖,而不訴諸function(y)
然而另一種方式,因爲我的名單往往是無名我傾向於列表的值添加爲名稱如下:
map_test<-function(list_in,...){
if (is.null(names(list_in))){
names(list_in)<-list_in
}
map(list_in,...)
}
cc2=map_test(ac,function(y) map_test(ca,~paste0(.x,y)))
df=reshape2::melt(cc2)
由此產生的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
變量這會導致混亂非常快。我可以創建1,2,3,4..n在不同的功能的這個版本,但
Q2:是否有可能創建一個 melt_map功能,將允許雙方接受遍歷正不同的列表和那最後還會把n個不同的列表名稱保存爲列名?