在R中并行化功能

时间:2019-07-02 19:57:49

标签: r

我正在对大约25k个短音频文件运行声音分析功能。我的代码可以运行,但是需要很长时间才能运行。什么是并行化的好方法?

非常感谢

files = list.files(getwd(), pattern=".mp3", all.files=FALSE, full.names=FALSE)

out=NULL

for (i in files) {

res <- try(soundgen::analyze(i,pitchMethods = 'dom',  plot = FALSE, summary = TRUE), silent = TRUE)
res["1", "duration"] <- i[[1]]
out=rbind(out,res)
print(i)
}

1 个答案:

答案 0 :(得分:1)

您可以使用并行程序包轻松实现此目的:

library(parallel)
library(soundgen)

files <- list.files(getwd(), pattern=".mp3", all.files=FALSE, full.names=FALSE)

out <- NULL

soundAnalysis <- function(file){
  res <- try(soundgen::analyze(file,pitchMethods = 'dom',  plot = FALSE, summary = TRUE), silent = TRUE)
  res["1", "duration"] <- file[[1]]
  out <- rbind(out,res)
  print(i)
}

output <- mclapply(X = files, FUN = soundAnalysis, mc.cores = detectCores())