||
前一段,写过一篇:R语言中写入Excel的不同sheet表格,最近学习了tidyverse的方法,感觉需要总结一下,更新一下知识结构。
本文准备用实际数据,做一下操作:
「批量读取:」
「批量写入」
模拟数据的过程很简单,新建一个Excel,里面加点内容,然后复制粘贴,重命名。
❝懂了这么多编程知识,这一步显得不够高科技,但是确实是很直接。
❞
「步骤:」
library(tidyverse) library(openxlsx) list_name = dir("./",pattern = ".xlsx") list_name re = map(list_name, ~ read.xlsx(.,sheet=1)) re names(re) = list_name re write.xlsx(re,"../re_hebing.xlsx")
目录:读取结果:结果文件:
在这里插入图片描述「步骤:」
name_sheet = 1:8 re2 = map(name_sheet, ~ read.xlsx("../re_hebing.xlsx",sheet=.)) re2
「步骤:」
这里,我们用re
的结果:
> str(re) List of 8 $ a1.xlsx:'data.frame': 4 obs. of 2 variables: ..$ ID: num [1:4] 1 2 3 4 ..$ y : num [1:4] 0.6275 0.4325 0.6046 0.0681 $ a2.xlsx:'data.frame': 4 obs. of 2 variables: ..$ ID: num [1:4] 1 2 3 4 ..$ y : num [1:4] 0.6275 0.4325 0.6046 0.0681 $ a3.xlsx:'data.frame': 4 obs. of 2 variables: ..$ ID: num [1:4] 1 2 3 4 ..$ y : num [1:4] 0.6275 0.4325 0.6046 0.0681 $ a4.xlsx:'data.frame': 4 obs. of 2 variables: ..$ ID: num [1:4] 1 2 3 4 ..$ y : num [1:4] 0.6275 0.4325 0.6046 0.0681 $ a5.xlsx:'data.frame': 4 obs. of 2 variables: ..$ ID: num [1:4] 1 2 3 4 ..$ y : num [1:4] 0.6275 0.4325 0.6046 0.0681 $ a6.xlsx:'data.frame': 4 obs. of 2 variables: ..$ ID: num [1:4] 1 2 3 4 ..$ y : num [1:4] 0.6275 0.4325 0.6046 0.0681 $ a7.xlsx:'data.frame': 4 obs. of 2 variables: ..$ ID: num [1:4] 1 2 3 4 ..$ y : num [1:4] 0.6275 0.4325 0.6046 0.0681 $ a8.xlsx:'data.frame': 4 obs. of 2 variables: ..$ ID: num [1:4] 1 2 3 4 ..$ y : num [1:4] 0.6275 0.4325 0.6046 0.0681
这里,我们将工作目录设置为上一级,用map2
进行操作:
setwd("../") sheet_name = names(re) sheet_name map2(re,sheet_name,write.xlsx)
结果:
这就不用操作,直接写入就行了,一行代码:
write.xlsx(re,"new_new_many_sheets.xlsx")
> 欢迎关注我的公众号:`育种数据分析之放飞自我`。主要分享R语言,Python,育种数据分析,生物统计,数量遗传学,混合线性模型,GWAS和GS相关的知识。
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-9-27 06:28
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社