`as.matrix`和`as.data.frame` S3方法与S4方法

cbe*_*ica 5 methods r s4

我注意到,定义as.matrixas.data.frame如S3方法的S4类使得如lm (formula, objS4)prcomp (object)工作的开箱.如果将它们定义为S4方法,则不起作用.

为什么将方法定义为S3或S4方法至关重要?

示例as.data.frame:

setClass ("exampleclass", representation (x = "data.frame"))
object <- new ("exampleclass", x = iris)

setMethod ("as.data.frame", signature="exampleclass", definition= function (x, ...) x@x )
## [1] "as.data.frame"

as.data.frame (object)
## Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
## 1            5.1         3.5          1.4         0.2     setosa
## 2            4.9         3.0          1.4         0.2     setosa
## 3            4.7         3.2          1.3         0.2     setosa
## ...snip...

lm (Petal.Length ~ Petal.Width, object)
## error in as.data.frame.default(data) : 
##   cannot coerce class 'structure("exampleclass", package = ".GlobalEnv")' into a data.frame

as.data.frame.exampleclass <- function (x, ...) x@x

lm (Petal.Length ~ Petal.Width, object)
## Call:
##   lm(formula = Petal.Length ~ Petal.Width, data = object)
## 
## Coefficients:
##   (Intercept)  Petal.Width  
## 1.084        2.230  
Run Code Online (Sandbox Code Playgroud)

由于情况可能有点复杂,lm只有在从数据构造的环境中计算公式时才会发生强制,这里有一个更简单的情况,即相同的行为:

setMethod ("as.matrix", signature="exampleclass", definition= function (x, ...) as.matrix (x@x[, 1:4]) )
prcomp (object)
## error in as.vector(data) : 
##   No method to coerce this S4 class into a vector
as.matrix.exampleclass <- function (x, ...) as.matrix (x@x [, 1:4])
prcomp (object)
##   Standard deviations:
##     [1] 2.0562689 0.4926162 0.2796596 0.1543862
##   
## Rotation:
##   PC1         PC2         PC3        PC4
## Sepal.Length  0.36138659 -0.65658877  0.58202985  0.3154872
## Sepal.Width  -0.08452251 -0.73016143 -0.59791083 -0.3197231
## Petal.Length  0.85667061  0.17337266 -0.07623608 -0.4798390
## Petal.Width   0.35828920  0.07548102 -0.54583143  0.7536574
Run Code Online (Sandbox Code Playgroud)

在这里,stats:::prcomp.default被称为,以平原开始x <- as.matrix (x).这与上面的S4定义失败,但适用于S3定义.

jam*_*son 2

lm我从简单明确调用的评论中获取它as.data.frame。如果你看as.data.frame

> as.data.frame
function (x, row.names = NULL, optional = FALSE, ...) 
{
    if (is.null(x)) 
        return(as.data.frame(list()))
    UseMethod("as.data.frame")
}
<bytecode: 0x29140b8>
<environment: namespace:base>
Run Code Online (Sandbox Code Playgroud)

您会看到它调用 S3 泛型,并且从方法文档中

如果直接调用 S3 泛型函数,则看不到单独的 S4 方法。但是,> 原始函数和运算符是例外:当且仅当对象是 S4 对象时,内部 C 代码才会查找 S4 > 方法。[在示例中,对于此类的对象,将始终调用 >class“myFrame”的方法。