Search code examples
rloopswavacoustics

Is there a way to change my file path automatically and apply a function in R?


I'm looking to apply the 'multiple_sounds' function to a list of .WAV files from a folder using R. This is my current code:

##### 1 #####
setwd("E:/Audiomoth Files/Winter/Rural/Emma/_1")
multiple_sounds(directory = "E:/Audiomoth Files/Winter/Rural/Emma/_1", resultfile = "ndsi_results.csv", soundindex = "ndsi", no_cores = "-2")
multiple_sounds(directory = "E:/Audiomoth Files/Winter/Rural/Emma/_1", resultfile = "adi_results.csv", soundindex = "acoustic_diversity", no_cores = "-2")
multiple_sounds(directory = "E:/Audiomoth Files/Winter/Rural/Emma/_1", resultfile = "aei_results.csv", soundindex = "acoustic_evenness", no_cores = "-2")
multiple_sounds(directory = "E:/Audiomoth Files/Winter/Rural/Emma/_1", resultfile = "aci_results.csv", soundindex = "acoustic_complexity", no_cores = "-2")
multiple_sounds(directory = "E:/Audiomoth Files/Winter/Rural/Emma/_1", resultfile = "H_results.csv", soundindex = "bioacoustic_index", no_cores = "-2")

##### 2 #####
setwd("E:/Audiomoth Files/Winter/Rural/Emma/_2")
multiple_sounds(directory = "E:/Audiomoth Files/Winter/Rural/Emma/_2", resultfile = "ndsi_results.csv", soundindex = "ndsi", no_cores = "-2")
multiple_sounds(directory = "E:/Audiomoth Files/Winter/Rural/Emma/_2", resultfile = "adi_results.csv", soundindex = "acoustic_diversity", no_cores = "-2")
multiple_sounds(directory = "E:/Audiomoth Files/Winter/Rural/Emma/_2", resultfile = "aei_results.csv", soundindex = "acoustic_evenness", no_cores = "-2")
multiple_sounds(directory = "E:/Audiomoth Files/Winter/Rural/Emma/_2", resultfile = "aci_results.csv", soundindex = "acoustic_complexity", no_cores = "-2")
multiple_sounds(directory = "E:/Audiomoth Files/Winter/Rural/Emma/_2", resultfile = "H_results.csv", soundindex = "bioacoustic_index", no_cores = "-2")

This works, but at the moment I have to copy and paste this code each time for each folder(_1, _2, _3 etc.) of which there are 623, so doing this manually is very time consuming.

Is there a way I can automate the file path to change the directory to go to _3, _4, _5 automatically and apply the function in a loop all the way to the 623rd folder?

Thank you in advance


Solution

  • I would suggest writing a utility function and using the directory as an input:

    apply_wav_index = function(
      dir,
      index = c("ndsi", "acoustic_diversity", "acoustic_evenness", "acoustic_complexity", "bioacoustic_index"),
      labels = c("ndsi", "adi", "aei", "aci", "H"),
      ...
    ) {
      if(length(index) != length(labels)) stop("Must provide same number of labels and indexes")
      for(i in seq_along(index)) {
        multiple_sounds(
          directory = dir,
          resultfile = paste0(dir, "/", labels[i], "_results.csv"),
          soundindex = index[i],
          ...
        )
      }
    }
    

    Then you should be able to call it like this:

    for(i in 1:6) {
      apply_wav_index(dir = paste0("E:/Audiomoth Files/Winter/Rural/Emma/_", i), no_cores = "-2")
    }
    

    You could also potentially generate the list of directories with list.dirs().