1

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

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?这个解决方案似乎不稳定。

4

1 回答 1

1

我才意识到这实际上是多么简单。嗬!

将 ' specS4 slotNames' 对象与names'spec' 对象匹配,然后赋值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
于 2013-01-03T06:14:57.177 回答