为了更加清晰,我附上了以下代码:我有一个名为grid的S4对象矩阵,每个对象都有一个状态和一个hashmap,如类定义中所示。S4对象深度复制
cell <- setClass(
# Set the name for the class
"cell",
# Define the slots
slots = c(
state = "numeric",
code = "hash"
),
# Set the default values for the slots.
prototype=list(
state = 1
)
)
setGeneric(name="copy_cell",
def= function(x, y){
standardGeneric("copy_cell")
})
setMethod(f = "copy_cell", signature = "cell",
definition = function(x, y) {
[email protected] = [email protected]
[email protected] = copy([email protected])
})
grid <- matrix(sapply(1:(20*20), function(x){
new("cell",
state = ifelse(runif(1)<= 0.5, 3, 1),
code = hash())
}), 20, 20)
我想复制这个网格的内容到nextgrid。因为该对象包含一个散列表(它是一个通过引用传递的环境),所以一个简单的:nextgrid = grid
将不会这样做。我做了对单独创建的细胞工程的功能:
cell1 = new("cell", state = ifelse(runif(1)<= 0.5, 3, 1), code = hash("a",1))
cell2 = new("cell")
cell2 = copy_cell(cell2, cell1)
,但我有一个很难实现的方式在矩阵的所有单元格应用功能。下面,我首先将网格复制到nextgrid中,以避免重新初始化另一个完整的对象矩阵,然后使用sapply。
nextgrid = grid
sapply(grid[,], function(x) copy_cell(nextgrid[,], x))
我得到的错误:错误(函数(类,FDEF,mtable):无法找到函数继承的方法“copy_cell”签字“‘矩阵’” 这我理解,因为我似乎要传递一个矩阵的功能,而不是单个细胞......但我不知道如何正确地重新写它...
我试着用R6对象重写我的类,有一个很好的克隆/深层克隆方法,但是我很难将我的矩阵切片为其他操作,所以我有点卡住了。
想法?
没错。你将如何创建一个当前设置的网格?并说我想确保第一行和最后一行中的所有单元格以及第一列和最后一列的状态= 1,是否有一种有效的方法可以做到这一点? –
只需向下滚动即可查看网格是如何创建的:)您可以使用'grid [20,] < - lapply(1:20,function(x)cell(x,hash()))'将值分配给最后一行。 –