注意区分直接使用reduce和使用reduce联合map的区别
方法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)
如果有一列矩阵列表,需要对矩阵列表进行合并,生成一个总的大矩阵列表,直接使用reduce函数就可以了,注意列表中矩阵的行数或者栏数需要相同
list_all = list(df1, df2, df3, df4)
combined_df = reduce(list_all, cbind)
debug类函数
- possibly()函数,类似tryCatch, 报错也能后继续执行循环
possible_sqrt <- possibly(sqrt, otherwise = NA_real_)
2.safely()函数,与possibly类似,但会在列表中返回列表。因此袁术是结果和伴随错误消息的列表。如果没有错误返回NULL,否则返回错误信息。