【发布时间】:2012-09-01 19:47:54
【问题描述】:
我正在R 中写一个邻接矩阵,如下所示:
neighbours <- array(0, c(100,100))
for (i in 1:100) { neighbours[i,i] = 1 } #reflexive
但后来我注意到class(neighbours) 是double matrix。这将占用更大的矩阵太多的空间。所以我想强制类型为integer,或者更好,因为这是无向的,logical。
但是……
> class(neighbours[5])
[1] "numeric"
> class(neighbours[5]) <- "integer"
> class(neighbours[5])
[1] "numeric"
不听我的!
【问题讨论】:
-
您可以使用
diag(neighbours) <- 1避免for循环。 -
这将是另一个问题,您可以这样发布。