R 迭代合并df list 2021-12-23

注意區(qū)分直接使用reduce和使用reduce聯(lián)合map的區(qū)別

方法1

Reduce(function(x,y) Map(cbind, x, y),list(one, two,three))

方法2

do.call(mapply, c(cbind,list(one, two, three)))

方法3

sep.list <- unlist(list(one, two, three), recursive = FALSE)
lapply(split(sep.list, names(sep.list)), do.call, what = cbind)

方法4

mapply(cbind,mapply(cbind,one,two,SIMPLIFY=FALSE),three,SIMPLIFY=FALSE)

方法五, 兩個list

list_all  <- purrr::map2(list1, list2, cbind)

Purrr map reduce 介紹

purrr的reduce語法

reduce(.x, .f, ..., .init, .dir = c("forward","backward")
reduce2(.x, .y, .f, ..., .init)

如果有一列矩陣列表,需要對矩陣列表進(jìn)行合并,生成一個總的大矩陣列表,直接使用reduce函數(shù)就可以了,注意列表中矩陣的行數(shù)或者欄數(shù)需要相同

list_all = list(df1, df2, df3, df4)
combined_df = reduce(list_all, cbind)

debug類函數(shù)

  1. possibly()函數(shù),類似tryCatch, 報錯也能后繼續(xù)執(zhí)行循環(huán)
possible_sqrt <- possibly(sqrt, otherwise = NA_real_)

2.safely()函數(shù),與possibly類似,但會在列表中返回列表。因此袁術(shù)是結(jié)果和伴隨錯誤消息的列表。如果沒有錯誤返回NULL,否則返回錯誤信息。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容