gpt4 book ai didi

r - 将 S4 R 对象分配给矩阵 : why does this work?

转载 作者:行者123 更新时间:2023-12-05 00:33:33 26 4
gpt4 key购买 nike

我定义了一个名为 cell 的 S4 类,我想分配给 3x3 矩阵的实例(选择 3x3 是为了确定性)。以下代码适用于 R 版本 2.15.1,并在简单情况下重现 R 的行为。我发现我可以分配 cell 类型的对象到一个矩阵,其条目首先被初始化为空列表 matrix(list(),3,3) ,之后我分配类型 cell 的新对象到条目。问题是:它为什么有效?

    setClass("cell", representation = representation(       
A="numeric", # a field
B="numeric")) # another one

# initialize the cell
setMethod("initialize", "cell", function(.Object, a,b) {
.Object@A <- a;
.Object@B <- b;
.Object})

createGrid <- function(a,b) {
grid <- matrix(list(),3,3) # note initialization to list()
for (i in 1:3 )
for (j in 1:3)
grid[[i,j]] <- new("cell",j,i);
grid}

这是一个示例 session :
    > source("stackoverflow.R")
> grid <- createGrid(1,2)
> grid[[1,3]]
An object of class "cell"
Slot "A":
[1] 3

Slot "B":
[1] 1

> grid[[2,3]]
An object of class "cell"
Slot "A":
[1] 3

Slot "B":
[1] 2

修改 createGrid()通过将空列表分配更改为 grid<- matrix(0,3,3)会产生一个错误:
    > grid <- createGrid0(1,2)
Error in grid[[i, j]] <- new("cell", j, i) :
more elements supplied than there are to replace

这并不奇怪,但它确实让我找到了工作代码。
以下尝试使用 new() 定义一个 3x3 的单元格矩阵失败:
    > grid <- matrix(new("cell",1,2),3,3)
Error in as.vector(data) :
no method for coercing this S4 class to a vector

问题是,为什么第一个有效?

最佳答案

没有真正回答这个问题,但是......

通常从向量的角度考虑是值得的,因此可能不是“单元格”,而是代表整个矩阵的“单元格”。这是一个实现。这个想法是 Cell 扩展矩阵,使用包含附加值的向量。

setClass("Cell", representation("matrix", A="numeric", B="numeric"))

我们用有效性函数约束 A 和 B 具有相同的长度
setValidity("Cell", function(object) {
msg <- NULL
if (length(object@A) != length(object@B))
msg <- c(msg, "'A' and 'B' must be the same length")
if (is.null(msg)) TRUE else msg
})

并创建一个构造函数,将带有索引的 Cell 矩阵初始化为 A 和 B。
Cell <- function(A, B, ...)
new("Cell", matrix(seq_along(A), ...), A=A, B=B)
...参数是 matrix() 的非数据参数(nrow、ncol、dimnames 等)。

我们免费获得了一些功能(例如,dim、nrow、ncol、length 等),但需要实现子设置和其他操作。对于单括号子设置,我们将操作向下传递到底层矩阵,然后使用结果索引对 A 和 B 进行子集,创建一个具有更新值的新 Cell 实例
setMethod("[", "Cell", function(x, i, j, ..., drop=TRUE) {
m <- callNextMethod()
initialize(x, m, A=x@A[m], B=x@B[m])
})

最后实现一个show方法
setMethod(show, "Cell", function(object) {
cat("class:", class(object), "\n")
cat("dim:", dim(object), "\n")
m <- object@.Data
m[] <- object@A
cat("A:\n"); print(m)
m[] <- object@B
cat("B:\n"); print(m)
})

然后在行动:
> Cell(1:6, 6:1, 3, 2)[c(3, 1), 2:1]
class: Cell
dim: 2 2
A:
[,1] [,2]
[1,] 6 3
[2,] 4 1
B:
[,1] [,2]
[1,] 1 4
[2,] 3 6

这种实现似乎有几个好处。矩阵应该具有单一类型,这里就是这种情况(使用 cell ,矩阵的元素是列表,因此可以包含任何数据)。从矩阵继承了相当多的功能。大概你想在你的 Cell 矩阵上实现操作,并且可以有效地操作底层向量 A,B,例如通过组通用 Arith,
setMethod("Arith", c("Cell", "Cell"), function(e1, e2) {
A <- callGeneric(e1@A, e2@A)
B <- callGeneric(e1@B, e2@B)
initialize(e1, e1@.Data, A=A, B=B)
})

进而
> c1 = Cell(1:6, 6:1, 3, 2)
> c2 = Cell(6:1, 1:6, 3, 2)
> c1 + c2
class: Cell
dim: 3 2
A:
[,1] [,2]
[1,] 7 7
[2,] 7 7
[3,] 7 7
B:
[,1] [,2]
[1,] 7 7
[2,] 7 7
[3,] 7 7
> c1 * c2
class: Cell
dim: 3 2
A:
[,1] [,2]
[1,] 6 12
[2,] 10 10
[3,] 12 6
B:
[,1] [,2]
[1,] 6 12
[2,] 10 10
[3,] 12 6

关于r - 将 S4 R 对象分配给矩阵 : why does this work?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11857658/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com