Nancy
Nancy

Reputation: 101

Loop for directories and files in R

I have a question about loop for my script. I have different directories and in each of them there are different files. My script analizes two files at the same time. I have for example file1, file2, file 3 and file 4. My script works analizing in couple file1-file2, then I have to do file1-file3, file1-file4, and file2-file3 and so on. So I have to analize each file with all the others without repetition. I was doing something like

dirs <- list.dirs()

for (d in dirs) {

files <- list.files()

a <- read.table("file1") ##what I have to write here? 
b <- read.table("file2")  ## and here?
dm <- dist(a, b , method = "euclidean")
write.table(dm, "file1-file2.csv")
}

My question is about calling file1 and file2 (and others too) after listed them. The format name file is like "1abc_A.txt" thank you :)

Upvotes: 0

Views: 1552

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389275

Try the following :

  • Use lapply to loop over each directory.
  • For each directory get all text filenames using list.files.
  • Create every combination of 2 text files and apply the dist function to every 2 files.
  • Write the output. If the directory is called A and filenames are f1 and f2. It should write a file called A_f1_f2.csv in the working directory.
dirs <- list.dirs()

lapply(dirs, function(y) {
  files <- list.files(y, pattern = '\\.txt') 
  if(length(files) < 2) return(NULL)

  combn(files, 2, function(x) {
    a <- read.table(x[1]) 
    b <- read.table(x[2])
    dm <- dist(a, b , method = "euclidean")
    write.table(dm, sprintf('%s_%s.csv', basename(y), paste0(x, collapse = '_')))
  }, simplify = FALSE)
})

Upvotes: 3

Related Questions