问题描述
我想根据两个文件的第 1 列将文件夹“1”中所有文件中的文件 dip.txt 一一合并。
我的代码如下:
setwd("~/Test/1")
require(tidyverse)
#read table (this folder 2 table need to place with every file present in folder 1 based on first column)
df1 <- read.table("~/Test/2/dip.txt",sep="\t",stringsAsFactors=FALSE,header=TRUE)
#I would like to run this script so that it read all file one by one present in folder 1 and save each output.
df2 <- read.table("5d98.txt",header=TRUE)
lst <- list(data.frame(df1),data.frame(df2))
df3 <- reduce(lst,full_join,by = "ID") %>% replace(.,is.na(.),0);
data.table::fwrite(df3,file="5d98output.txt",quote = F,sep = "\t",row.names = F)
文件夹 1 的文件名是随机数。我想一个一个打开文件夹 1 中的文件,进行我的数据操作,使用带有一些后缀或前缀的原始名称保存该文件,关闭它并继续下一个文件。
解决方法
我们可以使用
files <- list.files()
lst1 <- lapplyfiles,function(x) read.table(x,stringsAsFactors = FALSE,header = TRUE))
lapply(lst1,function(x) full_join(df1,x,by = 'ID') %>% replace(.,is.na(.),0))
,
要将其转换为 for 循环,请首先获取工作目录中的 .txt 文件列表:
myfiles <- list.files(pattern="*.txt")
然后循环遍历每个文件,读取、加入 df1,并对现有代码进行少量修改:
for (file in myfiles) {
df2 <- read.table(file,sep="\t",stringsAsFactors=FALSE,header=TRUE)
lst <- list(data.frame(df1),data.frame(df2))
df3 <- reduce(lst,full_join,by = "ID") %>% replace(.,0);
data.table::fwrite(df3,file=paste0("output_",file),quote = F,sep = "\t",row.names = F)
}