Ben Coral
Ben Coral

Reputation: 3

How to use ', pattern = ' to select files from a list of possible suffixes

I have 20'000 1minute wav files recorded over two weeks in a folder. These are all named with a suffix to indicate the time of day they were recorded (e.g "_213032" = 9:30pm and 32 seconds). I want to work on a smaller subset of these recorded at certain times of day (once every 20 minutes between 7pm and 5am). So I created a vector of the appropriate suffixes (file_name_ends) and saved all the file names I want into a vector (wav_files) using:

vector_of_files = file_name_ends[,1]
wav_files = vector()
for(i in 1:length(vector_of_files)){
  wav_files = c(wav_files, dir(path, pattern = vector_of_files[i]))
}

I then read in these wav files during a loop:

filenames = vector()
for_AR = vector()
for( i in 1:length(wav_files)){
  filenames = c(filenames,(wav_files[i])) #add the file name to empty list
  load_file = readWave(wav_files[i]) #select the wave file being iterated upon
  for_AR = c(for_AR, load_file)}

This component works well for other functions I need to run on this files in this loop. But, I now want to run the AR function from the seewave package on only the 470 .wav files that match one of these file names in the large folder of 20'000 files. I have attempted many variants similar to the following without success:

 ARvalues = AR(for_AR, datatype = "object", pattern = wav_files)
 #or 
 ARvalues = AR(path, datatype = "files", pattern = vector_of_files)

I suspect the key is in the correct use of the 'pattern = ' component of this function with one of my vectors?

In case this helps:

head(file_name_ends)
       V1
1 _190002
2 _192002
3 _194002
4 _200002
5 _202002
6 _204002

head(vector_of_files)
[1] "_190002" "_192002" "_194002" "_200002" "_202002" "_204002"

head(wav_files)
[1] "20210209_192002.WAV" "20210209_194002.WAV" "20210209_200002.WAV" "20210209_202002.WAV"

head(for_AR) #practicing on four files for now
[[1]]

Wave Object
    Number of Samples:      2784000
    Duration (seconds):     58
    Samplingrate (Hertz):   48000
    Channels (Mono/Stereo): Mono
    PCM (integer format):   TRUE
    Bit (8/16/24/32/64):    16 


[[2]]

Wave Object
    Number of Samples:      2784000
    Duration (seconds):     58
    Samplingrate (Hertz):   48000
    Channels (Mono/Stereo): Mono
    PCM (integer format):   TRUE
    Bit (8/16/24/32/64):    16 


[[3]]

Wave Object
    Number of Samples:      2784000
    Duration (seconds):     58
    Samplingrate (Hertz):   48000
    Channels (Mono/Stereo): Mono
    PCM (integer format):   TRUE
    Bit (8/16/24/32/64):    16 


[[4]]

Wave Object
    Number of Samples:      2784000
    Duration (seconds):     58
    Samplingrate (Hertz):   48000
    Channels (Mono/Stereo): Mono
    PCM (integer format):   TRUE
    Bit (8/16/24/32/64):    16 

Upvotes: 0

Views: 47

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388972

Try to call AR function with do.call.

library(seewave)
result <- do.call(AR, for_AR)
result

Upvotes: 1

Related Questions