从列表向量中提取 S4 对象

问题描述

我有一个包含来自 monitoR::makeCorTemplate 函数的 S4 相关模板的列表向量。

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 "" 

接下来的处理步骤是通过 combineCorTemplates 组合这 10 个模板:

> 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

我的问题,如何在不写出的情况下从列表向量中提取 S4 每个列表元素作为 combineCorTemplates(temps_0[[1]],& etc 因为这很容易出错。

解决方法

我们可以使用 do.call

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

-测试

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

注意:可重现的示例是从 ?combineCorTemplates

创建的

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...