R批量读取、处理及写出文件

在对同一路径下多个文件做相同处理时,可以循环读取文件夹中的文件,批量读取,处理和写入文件,会大大提高工作效率,在R语言中,处理方法如下所示。

1.批量读取文件

path <- "~/path/to/your/file/"
fileNames <- dir(path) 
filePath <- sapply(fileNames, function(x){ 
  paste(path,x,sep='/')})   
data <- lapply(filePath, function(x){
  read.csv(x)})  

2.批量处理文件及写出

上面多个文件被读入到一个叫data的list的文件中,下面可以通过data[[]]来取出每一个文件来进行相同的处理。

for (i in 1:length(fileNames)){
  temp<-data[[i]]
write.csv(temp,paste0("../../AllMatrixCountGeneSymbol/",tag,".csv"))
  
}

下面是对ensembl id注释成gene symbol的例子
每个需要处理的文件为:



注释文件:


for (i in 1:length(fileNames)){
  temp<-data[[i]]
temp[,1]<-unlist(lapply(as.character(temp[,1]), function(x){strsplit(x, "\\.")[[1]][1]}))
tag<-unlist(lapply(as.character(colnames(temp)[1]), function(x){strsplit(x, "\\id_")[[1]][2]}))

colnames(temp)[1]<-"ENSEMBL"
temp<-aggregate(.~ENSEMBL,temp,mean)
temp<-merge(ann,temp)
temp<-temp[,-1]
temp<-aggregate(.~GeneSymbol,temp,mean)
rownames(temp)<-temp$GeneSymbol
temp<-temp[,-1]
write.csv(temp,paste0("../../AllMatrixCountGeneSymbol/",tag,".csv"))
  
}

欢迎关注~

参考:
https://blog.csdn.net/u011596455/article/details/79601113

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

友情链接更多精彩内容