0

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

HarHar
  • 35
  • 5
  • You can use `list.files` to get a list of files in advance, you can use `paste()` or `sprintf` or `glue::glue` to create the filepaths programmatically... – Gregor Thomas Apr 22 '21 at 15:41

1 Answers1

3

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().

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • Thank you so much for your help I really appreciate it! But could you please explain to a complete beginner what else I need to input to get this to work as I've run the code but it didn't do anything, I expect its because I need to alter the code somewhat but I've very new to R. I sincerely apologise for my ineptitude. – HarHar Apr 22 '21 at 18:06
  • The first code block defines a function. It doesn't try to do anything other than create the `apply_wav_index` function in your workspace. The second code block starting with `for(i in 1:6)` runs the function on `...Emma/_1`, `.../Emma/_2`, ..., `...Emma/_6`. It should work, creating your output files. (Check the timestamps on the files to see if they are recreated.) If it works for `i in 1:6`, you could then try editing it to `i in 7:623`, or however many files you have. – Gregor Thomas Apr 22 '21 at 18:31
  • When I run the `for (i in 1:6)` part, I get this message `Error: unexpected '}' in: " apply_wav_index(dir = paste0("E:/Audiomoth Files/Winter/Rural/Emma/_", i, no_cores = "-2") }"` I have tried to remove the `}` but R appears to do nothing... could it be because the `multiple_sounds` function is just taking a long time to run? – HarHar Apr 22 '21 at 20:13
  • No, that's a syntax error because I couldn't test the code without anything to test on. – Gregor Thomas Apr 22 '21 at 20:19
  • I missed a `)`, should be fixed now. – Gregor Thomas Apr 22 '21 at 20:20