Merge multiple .txt files from multiple directories in R

谁说我不能喝 提交于 2019-12-25 18:48:25

问题


Merge .txt files from different sub directories

I have a folder that is filled with sub folders of past dates (01_14 for example), inside each date folder there are 11 files named 01.txt, 02.txt... How can I merge all the .txt files into one data frame, with one column with the name of the folder from where it came from and a column with the name of file from where it came from?

My hierarchy would look something like this:

\Data
     \01_14
           01.txt
           02.txt
           ...
           11.txt 
     \02_14
           01.txt
           02.txt
           ...
           11.txt 
     \03_14
           01.txt
           02.txt
           ...
           11.txt 

回答1:


When I need to read multiple files, i use a read.stack helper function which is basically a wrapper to read.table but it allows you to also add extra columns on a per-file basis. Here's how I might use it with your scenario.

dir<-"Data"

subdir<-list.dirs(dir, recursive=F)

#get dir/file names
ff<-do.call(rbind, lapply(subdir, function(x) {
    ff<-list.files(x, "\\.txt$", include.dirs = FALSE, full.names = TRUE)
    data.frame(dir=basename(x), file=basename(ff), 
        fullpath=ff, stringsAsFactors=F)
}))

#read into data.frame
read.stack(ff$fullpath, extra=list(file=ff$file, dir=ff$dir))



回答2:


Try this:

fileNames <- list.files("Data", recursive = TRUE, full.names = TRUE)
fileContents <- lapply(fileNames, function(fileName) 
  paste(readLines(fileName, warn = FALSE), collapse = "\n"))
meta <- regmatches(fileNames, regexec(".*Data/(.*)/(.*)$", fileNames))
merged <- mapply(c, fileContents, lapply(meta, "[", -1), SIMPLIFY = FALSE)
as.data.frame(t(do.call(cbind, merged)))


来源:https://stackoverflow.com/questions/25167187/merge-multiple-txt-files-from-multiple-directories-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!