批量读取文件夹下所有excel文件里的内容,放入列表 把12个excel读取,放入一个list里面 把文件夹下所有的excel里的所有内容分别读取,放入一个list
library(patchwork)library(ggplot2)library(ggalluvial)library(svglite)library(Seurat)library(openxlsx)getwd()path = "G:/silicosis/sicosis/yll/overlapped_clusters_0228/"dir.create(path)setwd(path)getwd()
**
文件内容如下**
#方式二library(openxlsx)path<-"G:/silicosis/sicosis/yll/overlapped_clusters_0228/just_overlapped_noothers" #设置读取路径files <- list.files(path, pattern = "*.xlsx$", full.names = TRUE) #读取xlsx文件名, pattern的值是正则表达式files
文件夹下有如下几个excel
dat<-list() for(i in 1:length(files)) {dat<-c(dat,list(read.xlsx(files[[i]],colNames = TRUE))) #读取Excel文件并合并到列表}![在这里插入图片描述](https://img-/86a4d755bcb845cfaefb028621397972.png)
把12个excel读取,放入一个list里面
把文件夹下所有的excel里的所有内容分别读取,放入一个list
**
结果展示
**
如果按照以下方法读取,则会得到一个dataframe
#方式三library(openxlsx)path<-"..." #设置读取路径pathfiles <- list.files(path, pattern = "*.xlsx$", full.names = TRUE) #读取xlsx文件名, pattern的值是正则表达式filesdat<-sapply(files,read.xlsx)head(dat)
**
如果按照以下方式读取,则可以把所有的excel里的内容都按行堆积合并成一个大的excel表格
**
getwd()setw("G:/silicosis/sicosis/yll/overlapped_clusters_0228/just_overlapped_noothers")files = list.files("./", pattern = "xlsx", full.names = TRUE)filesdf = map_dfr(files, read_xlsx) # 批量读取+按行堆叠合并df
结果展示如下
**
x下面这算法和前面算法很像,输出时候,也可以把所有的excel里的内容都按行堆积合并成一个大的excel表格 但是在r中view结果时候,是显示有很多na的 把所有不管行列名如何excel合并成一个大的excel
**
getwd()file="G:/silicosis/sicosis/yll/overlapped_clusters_0228/just_overlapped_noothers_po.q_o.7foldchage"dir.create(file)setwd(file)#方式二library(openxlsx)path<-"G:/silicosis/sicosis/yll/overlapped_clusters_0228/just_overlapped_noothers_po.q_o.7foldchage" #设置读取路径files <- list.files(path, pattern = "*stat.xlsx$", full.names = TRUE) #读取xlsx文件名, pattern的值是正则表达式files
dat<-list() for(i in 1:length(files)) {dat<-c(dat,list(read.xlsx(files[[i]],colNames = FALSE))) #读取Excel文件并合并到列表}dim(dat)datlength(dat)dat[[2]][1:4,1:4]dat[[4]][1:4,1:4]a=list()for (i in 1:11) {print(paste0("dat","[","[",i,"]","]"))a[[i]]=paste0("dat","[","[",i,"]","]")}a[1:11]#再把刚制作好的列表合并mydata=dplyr::bind_rows(dat[[1]],dat[[2]],dat[[3]],dat[[4]],dat[[5]],dat[[6]],dat[[7]],dat[[8]],dat[[9]],dat[[10]],dat[[11]])head(mydata,10)
dim(dat[[1]])dim(dat[[2]])glimpse(dat[[1]])dim(mydata)glimpse(mydata)View(mydata)View(dat[[1]])View(dat[[2]])getwd()setwd(file)getwd()write.xlsx(mydata,"all_mymerged_markers_rigion_interacted_clusters.xlsx")
j结果展示
批量读取文件夹下所有excel文件里的内容 放入列表 把所有不管行列名如何excel合并成一个大的excel 批量读取excel 批量合并excel