简体   繁体   English

使用循环在R中合并.txt文件

[英]Combining .txt files in R using a loop

I'm currently trying to use R to combine dozens of .txt files into one single .txt file. 我目前正在尝试使用R将数十个.txt文件合并为一个单个.txt文件。 Attached below is the code that I've been experimenting with so far. 下面随附的是到目前为止我一直在尝试的代码。 The files that I'm trying to combine have very similar names, for example: "e20171ny0001000.txt" and "e20171ct0001000.txt". 我要合并的文件具有非常相似的名称,例如:“ e20171ny0001000.txt”和“ e20171ct0001000.txt”。 As you can see, the only difference in the file names are the different state abbreviations. 如您所见,文件名的唯一区别是不同的状态缩写。 This is why I've been trying to use a for loop, in order to try to go through all the state abbreviations. 这就是为什么我一直尝试使用for循环,以便尝试遍历所有状态缩写的原因。

setwd("/Users/tim/Downloads/All_Geographies")

statelist = c('ak','al','ar','az','ca','co','ct','dc','de','fl','ga','hi','ia','id','il','in','ks','ky','la','ma','md','me','mi','mn','mo','ms','mt','nc','nd','ne','nh','nj','nm','nv','ny','oh','ok','or','pa','ri','sc','sd','tn','tx','ut','va','vt','wa','wi','wv','wy')

for (i in statelist){
  file_names <- list.files(getwd())
  file_names <- file_names[grepl(paste0("e20171", i, "0001000.txt"),file_names)]
  files <- lapply(file_names, read.csv, header=F, stringsAsFactors = F)
  files <- do.call(rbind,files)
}

write.table(files, file = "RandomFile.txt", sep="\t")

When I run the code, there isn't a specific error that pops up. 当我运行代码时,没有弹出特定错误。 Instead the entire code runs and nothing happens. 而是运行整个代码,什么也没有发生。 I feel like my code is missing something that is preventing it from running correctly. 我觉得我的代码缺少一些阻止其正常运行的内容。

We need to create a list to update. 我们需要创建一个列表进行更新。 In the OP's code, files is a list of data.frame that gets updated in the for loop. 在OP的代码中, filesdata.framelist ,该listfor循环中得到更新。 Instead, the output needss to be stored in a list . 相反,需要将输出存储在list For this, we can create a list of NULL 'out' and then assign the output to each element of 'out' 为此,我们可以创建一个NULL'out' list ,然后将输出分配给'out'的每个元素

out <- vector('list', length(statelist))
for (i in seq_along(statelist)){
  file_names <- list.files(getwd())
  file_names <- file_names[grepl(paste0("e20171", statelist[i], 
              "0001000.txt"),file_names)]
  files <-  lapply(file_names, read.csv, header=FALSE, stringsAsFactors = FALSE)
   out[[i]] <- do.call(rbind, files)


  }

As out is a list of data.frame , we need to loop over the list and then write it back to file 由于out是一个listdata.frame ,我们需要遍历所有的list ,然后将它写回文件

newfilenames <- paste0(statelist, "_new", ".txt")
lapply(seq_along(out), function(i) write.table(out[[i]],
      file = newfilenames[i], quote = FALSE, row.names = FALSE))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM