Reputation: 85
I have a series of files in a folder that look like this;
B_1.csv, B_1_2.csv, B_2.csv, B_2_2.csv, B_3.csv, B_4.csv, B_4_1.csv
Basically, I wish to merge any files which contain '_2' to their proceeding number (i.e. B_1_2.csv merges with B_1.csv). A further complication is that some files (such as B_3.csv) do not have a second file (_2) and therefore need to be ignored. I cannot think of an easy way of completing this. Any help would be greatly appreciated. Many thanks
Upvotes: 1
Views: 281
Reputation: 146154
Untested, of course, but this should work (or something close to it):
# identify CSV files
files = list.files(pattern = "csv$")
# look for ones that need merging
to_merge = grep("[0-9]_2\\.csv", files, value = TRUE)
# identify what they should be merged to
merge_target_file = sub("_2.csv", ".csv", to_merge, fixed = TRUE)
# make sure those exist
problems = setdiff(merge_target_file, files)
if(length(problems)) stop(problems, " not found, need merge targets.")
# read in the data
merge_target = lapply(merge_target_file, read.csv, stringsAsFactors = FALSE)
to_merge = lapply(to_merge, read.csv, stringsAsFactors = FALSE)
# merge and write
for(i in seq_along(merge_target)) {
write.csv(rbind(merge_target[[i]], to_merge[[i]]), file = merge_target_file[i])
}
Upvotes: 1