微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

r中的循环函数读取和保存多个数据文件

如何解决r中的循环函数读取和保存多个数据文件

我想根据两个文件的第 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)
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。