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

注意区分直接使用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类函数

  1. possibly()函数,类似tryCatch, 报错也能后继续执行循环
possible_sqrt <- possibly(sqrt, otherwise = NA_real_)

2.safely()函数,与possibly类似,但会在列表中返回列表。因此袁术是结果和伴随错误消息的列表。如果没有错误返回NULL,否则返回错误信息。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容