将交叉距离矩阵从 class "Crossdist" 转换为矩阵

Convert a cross-distance matrix from class "Crossdist" to a matrix

我使用代理包计算了以下交叉距离矩阵,并想将其用作矩阵对象。但是,crossdistance 是 class “crossdist” 并且使用 crossdistance <- as.matrix(crossdistance) 不会将其转换为矩阵,而是仍然是 class “crossdist”。欢迎任何评论:)

mat1 <- matrix(c(1,1,1,2,2,2),ncol=1)
mat2 <- matrix(c(1,2,2,2,3,3),ncol=1)

(crossdistance <- proxy::dist(mat1,mat2,type="euclidean"))

输出:

     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    0    1    1    1    2    2
[2,]    0    1    1    1    2    2
[3,]    0    1    1    1    2    2
[4,]    1    0    0    0    1    1
[5,]    1    0    0    0    1    1
[6,]    1    0    0    0    1    1


str(crossdistance)

输出:

 'crossdist' num [1:6, 1:6] 0 0 0 1 1 1 1 1 1 0 ...
 - attr(*, "method")= chr "Euclidean"
 - attr(*, "call")= language proxy::dist(x = mat1, y = mat2, type = "euclidean")

也许你可以试试

crossdistance <- `attr<-`(
  as.matrix(
    as.data.frame.matrix(crossdistance)
  ), "dimnames", NULL
)

你会看到

> crossdistance
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    0    1    1    1    2    2
[2,]    0    1    1    1    2    2
[3,]    0    1    1    1    2    2
[4,]    1    0    0    0    1    1
[5,]    1    0    0    0    1    1
[6,]    1    0    0    0    1    1

> str(crossdistance)
 num [1:6, 1:6] 0 0 0 1 1 1 1 1 1 0 ...

我们也可以使用dim<-

crossdistance2 <- `dim<-`(c(crossdistance), dim(crossdistance))