Reputation: 91
I'm asking help after several hours of experimenting with different code. I have a folder, with five subfolders. Each subfolder has three csv files. I want to cbind those three csv files in each subfolder and output the results in same subfolder, do same for each subfolder. So, in addition to the original 15 csv files, I'll have five combined files in those five subfolders. I appreciate your help. I put the following code lines together, but did not succeed
#Folder containing sub-folders
parent.folder <- "path"
# Sub-folders
sub.folders <- list.dirs(parent.folder, recursive=TRUE)[-1]
# List files in all subfolders
files <- sapply(sub.folders, list.files, all.files = F, full.names = T, recursive=TRUE)
# Make a list of lists
mydata <- lapply(files, function(x) read.csv(x, header = T)[,14:17]) #list of lists, each has 4 variables
for (r in 1:length(mydata)){
fileatt <- paste("path","new_file",r,".csv",sep="")
write.table(mydata[r],fileatt, row.name=F, col.name=c("a","b","c","d"), quote=F,sep=",")
}
Upvotes: 0
Views: 129
Reputation: 70653
This is the way I would approach it. Below is a fully working example which will create three sub-folders in a topdir
of your choice. It will populate each sub-folder with two text files with two columns and three lines.
The next step is finding these sub-folders, importing the files, merging them by sub-folder and writing to top directory.
## data generation
# top folder
topdir <- "test"
dir.create(topdir)
# create subfolders
subdirs <- c("sub1", "sub2", "sub3")
sapply(subdirs, FUN = function(x, topdir) dir.create(file.path(topdir, x)), topdir = topdir)
finddirs <- list.dirs(topdir, recursive = FALSE)
sapply(finddirs, FUN = function(x) {
replicate(3, {
fn <- sample(letters, 5, replace = TRUE)
fn <- paste(paste(fn, collapse = ""), ".txt", sep = "")
xy <- data.frame(a = 1:3, b = runif(3))
write.table(xy, file = file.path(x, fn), row.names = FALSE, sep = "\t")
})
})
## merging of files
# find subfolders
find.dirs <- list.dirs(topdir, recursive = FALSE)
# find files in dirs
files.in.dirs <- sapply(find.dirs, FUN = function(x) list.files(x, full.names = TRUE), simplify = FALSE)
# read files and merge into one data.frame
merged.by.subdir <- sapply(files.in.dirs, FUN = function(x) {
xy <- sapply(x, read.table, sep = "\t", simplify = FALSE, header = TRUE)
as.data.frame(do.call(rbind, xy))
}, simplify = FALSE)
# create main filenames per subfolder
bn <- basename(names(merged.by.subdir))
bn <- paste(bn, ".txt", sep = "")
bn <- file.path(topdir, bn)
# write data into folder
mapply(as.list(bn), merged.by.subdir, FUN = function(x, y) {
write.table(y, file = x, row.names = FALSE)
})
Upvotes: 1