Naming files in R loop

2020-04-21 02:50发布

I have multiple audio files which are held in several subfolders in my working directory. I have a loop which reads in the first minute of each file and then saves them as a new file.

library(tuneR)

dir.create("New files")

FILES<-list.files(PATH, pattern = "audio", recursive = TRUE)

for(i in 1:length(FILES)){
  OneMIN <- readWave(FILES[i], from = 0, to = 60, units = "seconds")
  writeWave(OneMIN, filename = (file=paste0(FILES[i], "_1-min.wav")))
}

Now, there are a couple of things wrong with this;

1) The new files are called e.g. "File1.wav_1-min.wav", so that I need to rename them to "File_1-min.wav"

2) The new files are in multiple subfolders, and I have to then copy them to the "New files" folder using additional steps outside the loop.

I have workarounds to resolve these issues, but I'm sure there is a more elegant way to do this, by including additional lines in the loop. I would like to:

1) Strip out the first '.wav' in the file name

2) Automatically save them to the "New files" folder

However, I don't have an idea about how to go about this. Any suggestions are appreciated. Thanks.

标签: r loops audio
2条回答
混吃等死
2楼-- · 2020-04-21 03:33

The file.path() function can be used to combined directories and filenames into a single filepath, while the sub() function will allow you to easily modify the filenames:

library(tuneR)

dir.create("New files")

FILES <- list.files(PATH, pattern = "audio", recursive = TRUE)

for(infile in FILES){
  OneMIN <- readWave(infile, from = 0, to = 60, units = "seconds")
  outfile <- file.path("New files", sub('.wav', '_1-min.wav', infile))
  writeWave(OneMIN, filename=outfile)
}

Also, it's worth noting that in the original code sample, the list.files() function will only return the filename part of each filepath.

Thus, you may need to modify your code to look something like:

FILES <- list.files(PATH, pattern = "audio", recursive = TRUE, full.names=TRUE)

and:

outfile <- file.path("New files", sub('.wav', '_1-min.wav', basename(infile)))

This will ensure that both infile and outfile are pointing to the correct locations.

查看更多
男人必须洒脱
3楼-- · 2020-04-21 03:37

Thought I would post my solution, which borrows from both @Keith Hughitt 's answer and @lmo 's comments. Thanks to both of them for helping work out what I needed.

for(i in 1:length(FILES)){
 OneMIN <- readWave(FILES[i], from = 0, to = 60, units = "seconds")
 FILE.OUT <- sub("\\.wav$", "_1 min.wav", FILES)
 OUT.PATH <- file.path("New files", basename(FILE.OUT))
 writeWave(OneMIN, filename = OUT.PATH[i])
}

As both implied, I was getting errors because of issues with the paths. I didn't need full.names = TRUE in the FILES assignment, but I did need to include basename() in the loop.

As Keith demonstrated in his answer, the FILE.OUT line is not strictly necessary, but it helps me to keep track of the different steps.

查看更多
登录 后发表回答