有没有办法自动更改我的文件路径并在 R 中应用函数?
Is there a way to change my file path automatically and apply a function in R?
我希望使用 R 将 'multiple_sounds' 函数应用于文件夹中的 .WAV 文件列表。这是我当前的代码:
##### 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")
这行得通,但目前我必须每次为每个文件夹(_1、_2、_3 等)复制并粘贴此代码,其中有 623 个,因此手动执行此操作非常耗时。
有没有一种方法可以自动更改文件路径以自动将目录更改为_3、_4、_5,并将函数循环应用到第 623 个文件夹?
提前致谢
我建议编写实用程序函数并使用目录作为输入:
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],
...
)
}
}
那么你应该可以这样调用它:
for(i in 1:6) {
apply_wav_index(dir = paste0("E:/Audiomoth Files/Winter/Rural/Emma/_", i), no_cores = "-2")
}
您还可以使用 list.dirs()
.
生成目录列表
我希望使用 R 将 'multiple_sounds' 函数应用于文件夹中的 .WAV 文件列表。这是我当前的代码:
##### 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")
这行得通,但目前我必须每次为每个文件夹(_1、_2、_3 等)复制并粘贴此代码,其中有 623 个,因此手动执行此操作非常耗时。
有没有一种方法可以自动更改文件路径以自动将目录更改为_3、_4、_5,并将函数循环应用到第 623 个文件夹?
提前致谢
我建议编写实用程序函数并使用目录作为输入:
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],
...
)
}
}
那么你应该可以这样调用它:
for(i in 1:6) {
apply_wav_index(dir = paste0("E:/Audiomoth Files/Winter/Rural/Emma/_", i), no_cores = "-2")
}
您还可以使用 list.dirs()
.