将'spec'S2类转换为新的S4类

时间:2013-01-02 21:01:48

标签: r class s4 spectrum

R我想转换(强制?)从stats::spectrum(类'spec')返回的对象到新的S4类中。 S3类'spec'本质上是一个混合格式的各种信息列表(我已经评论了屏幕输出):

psd3 <- spectrum(rnorm(1e3), plot=FALSE)
summary(psd3)
#           Length Class  Mode     
# freq      500    -none- numeric  
# spec      500    -none- numeric  
# coh         0    -none- NULL     
# phase       0    -none- NULL     
# kernel      0    -none- NULL     
# df          1    -none- numeric  
# bandwidth   1    -none- numeric  
# n.used      1    -none- numeric  
# orig.n      1    -none- numeric  
# series      1    -none- character
# snames      0    -none- NULL     
# method      1    -none- character
# taper       1    -none- numeric  
# pad         1    -none- numeric  
# detrend     1    -none- logical  
# demean      1    -none- logical 

class(unclass(psd3))
# [1] "list"

is.object(psd3) & !isS4(psd3)
# [1] TRUE

现在假设我们为名为'specS4'的类定义一个新的S4生成器,其中插槽名称是'spec'对象中的名称

specS4 <- setClass("specS4",
  representation = representation(freq="numeric", spec="numeric", coh="numeric", phase="numeric", kernel="numeric", df="numeric", bandwidth="numeric", n.used="numeric", orig.n="numeric", series="character", snames="character", method="character", taper="numeric", pad="numeric", detrend="logical", demean="logical"), 
  prototype = prototype(coh=numeric(0), phase=numeric(0), kernel=numeric(0), df=Inf, snames="", detrend=FALSE, demean=FALSE)
)

并从中生成一个新对象:

psd4 <- specS4()

validObject(psd4)
# [1] TRUE

psd3的每个组件分配到psd4中相应的广告位的最佳方法是什么? 一个复杂因素是spectrum可能会为少数(已知)字段返回NULL;分配这些值会在checkSlotAssignment中引发错误(对于给定的表示)。

我有一个痛苦的解决方案是:

nonull.spec <- function(psd){
  stopifnot(inherits(psd, 'spec', FALSE))
  # as.numeric(NULL) --> numeric(0)
  # spec.pgram/.ar both may return NULL for these:
  psd$coh <- as.numeric(psd$coh)
  psd$phase <- as.numeric(psd$phase)
  psd$kernel <- as.numeric(psd$kernel)
  psd$snames <- as.character(psd$snames)
  return(psd)
}

as.specS4 <- function(psd) UseMethod("as.specS4")
as.specS4.spec <- function(psd){
  stopifnot(inherits(psd, 'spec', FALSE))
  ## deal with possible NULLs
  psd <- nonull.spec(psd)
  ## generate specS4 class
  S4spec <- specS4()
  ## (re)assign from 'spec' list
  S4spec@freq <- psd$freq
  S4spec@spec <- psd$spec
  S4spec@coh <- psd$coh
  S4spec@phase <- psd$phase
  S4spec@kernel <- psd$kernel
  S4spec@snames <- psd$snames
  # [more to assign, obviously]
  # 
  # [run a validity check...]
  #
  return(S4spec)
}

哪个有效,即使as.specS4.spec故意不完整。

psd4c <- as.specS4(psd3)

validObject(psd4c)
# [1] TRUE

有没有更好的方法来实现as.specS4.spec的功能?这个解决方案似乎不稳定。

1 个答案:

答案 0 :(得分:1)

我刚刚意识到这实际上是多么简单。 卫生署!

将'specS4'对象中的slotNames与'spec'对象中的names匹配,然后使用slot进行匹配(假设问题中的代码已经运行):

as.specS4.spec <- function(psd){
  stopifnot(inherits(psd, 'spec', FALSE))
  psd <- nonull.spec(psd)
  S4spec <- specS4() 
  spec_slots <- slotNames(S4spec)
  spec_slots <- spec_slots[match(names(psd), spec_slots)]
  for (what in spec_slots){
    slot(S4spec, what) <- as.vector(unlist(psd[what]))
  }
  return(S4spec)
}

psd4c2 <- as.specS4(psd3)

validObject(psd4c2)
# [1] TRUE
> all.equal(psd4c@freq, psd4c2@freq, psd3$freq)
# [1] TRUE