Naming files in R loop

后端 未结 2 373
生来不讨喜
生来不讨喜 2021-01-23 13:53

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

2条回答
  •  别那么骄傲
    2021-01-23 14:31

    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.

提交回复
热议问题