简体   繁体   中英

Extract S4 object from list vector monitoR

I have a list vector containing S4 correlation templates from the monitoR::makeCorTemplate function.

temps_0 <- vector(mode = 'list',length=length(tru_files_info_0$species_id))
for (j in 1: length(tru_files_info_0$species_id)) {
temps_0[j] <- MonitoR::makeCorTemplate(paste0('tru_tp_files','/',paste0(tru_files_info_0$recording_id[j],'',
'.wav')), t.lim =c(tru_files_info_0$t_min[j], tru_files_info_0$t_max[j]), 
frq.lim = c(tru_files_info_0$f_min[j]/1000, tru_files_info_0$f_max[j]/1000), 
select = 'auto', dens =1, score.cutoff = 0.2, name = tru_files_info_0$new_name[j])
+ }```

Resulting object

Formal class 'corTemplateList' [package "monitoR"] with 1 slot
  ..@ templates:List of 1
  .. ..$ 00d442df7_0:Formal class 'corTemplate' [package "monitoR"] with 15 slots
  .. .. .. ..@ clip.path   : chr "tru_tp_files/00d442df7.wav"
  .. .. .. ..@ samp.rate   : int 48000
  .. .. .. ..@ pts         : num [1:1924, 1:3] 1 1 1 1 1 1 1 1 1 1 ...
  .. .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. .. ..$ : NULL
  .. .. .. .. .. ..$ : chr [1:3] "t" "frq" "amp"
  .. .. .. ..@ t.step      : num 0.0107
  .. .. .. ..@ frq.step    : num 0.0938
  .. .. .. ..@ n.t.bins    : int 73
  .. .. .. ..@ first.t.bin : num 19.4
  .. .. .. ..@ n.frq.bins  : int 25
  .. .. .. ..@ duration    : num 0.779
  .. .. .. ..@ frq.lim     : num [1:2] 5.91 8.25
  .. .. .. ..@ wl          : int 512
  .. .. .. ..@ ovlp        : int 0
  .. .. .. ..@ wn          : chr "hanning"
  .. .. .. ..@ score.cutoff: num 0.2
  .. .. .. ..@ comment     : chr "" 

The next processing step is to combine these 10 templates via combineCorTemplates:

> ctemps_0 <- monitoR::combineCorTemplates(temps_0[[1]], temps_0[[2]], temps_0[[3]], temps_0[[4]], temps_0[[5]], temps_0[[6]], temps_0[[7]], temps_0[[8]], temps_0[[9]], temps_0[[10]])
> ctemps_0

Object of class "corTemplateList"
    containing  10  templates
                    original.recording sample.rate lower.frequency
00d442df7_0 tru_tp_files/00d442df7.wav       48000           5.906
0ea8ea68a_0 tru_tp_files/0ea8ea68a.wav       48000           5.906
2e40b2294_0 tru_tp_files/2e40b2294.wav       48000           5.906
45c356538_0 tru_tp_files/45c356538.wav       48000           5.906

My question, how to extract the S4 from the list vector without writing out each list element as combineCorTemplates(temps_0[[1]], temps_0[[2]], & etc as this is error prone.

We could use do.call

c_temps_0 <- do.call(monitoR::combineCorTemplates, temps_0)

-testing

c_temps_1 <- combineCorTemplates(temps_0[[1]], temps_0[[2]], 
       temps_0[[3]], temps_0[[4]])

identical(c_temps_0, c_temps_1)
#[1] TRUE

NOTE: The reproducible example is created from ?combineCorTemplates

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM