注意區(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ù)
- possibly()函數(shù),類似tryCatch, 報錯也能后繼續(xù)執(zhí)行循環(huán)
possible_sqrt <- possibly(sqrt, otherwise = NA_real_)
2.safely()函數(shù),與possibly類似,但會在列表中返回列表。因此袁術(shù)是結(jié)果和伴隨錯誤消息的列表。如果沒有錯誤返回NULL,否則返回錯誤信息。