'Compare files in different folders in R
I am comparing files with same names in different folders which is easy if I just do it one by one. But how to compare multiple files in different folders with out doing it one by one. And write the output to an excel file in different sheets. such that abc-12.xlsx will contain 3 sheets where
sheet1= diff(~/my/path1/abc-12,~/my/path2/abc-12 )
sheet1= diff(~/my/path2/abc-12,~/my/path3/abc-12 )
sheet1= diff(~/my/path1/abc-12,~/my/path3/abc-12 )
I have files like below
Path:~/my/path1/
abc-12
abc-34
abc-45
abc-56
Path:~/my/path2/
abc-12
abc-45
Path:~/my/path3/
abc-12
abc-45
abc-56
#This is what I am trying
library(daff)
original <- list.files("~/my/path1/", ".tsv")
updated <- list.files("~/my/path2/", ".tsv")
edited <- list.files("~/my/path3/", ".tsv")
##Check if filename matches between two lists
check_updated <- basename(updated) %in% basename(original)
comp <- updated[check_updated]
if(comp == TRUE)
mapply(diff_data, original, updated)
Solution 1:[1]
I would say convert all folders' file name data as list and then extract the common between.
get_list <- function(path){
all_files <- c(paste0(path, list.files(path, recursive = TRUE)))
all_files <- substr(all_files, nchar(path)+1,nchar(all_files))
return(all_files)
}
Original <- get_list('/my/path1/foldername')
Updated<- get_list('/my/path2/foldername')
Edited <- get_list('/my/path3/foldername')
common_filenames <- intersect(intersect(Original,Update),Edited)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Rajan |
