将交叉距离矩阵转换为距离矩阵

时间:2020-12-31 12:55:06

标签: r distance

我有一个 crossdist 对象 d,我使用包 proxy 作为

# data is a data frame of 5000 rows and 5 columns
# Find dtw distance between rows in data

d <- proxy::dist(data, method = "dtw_basic", normalize = FALSE)

    str(d)
 'crossdist' num [1:5000, 1:5000] 0 3.72 6.38 2.66 3.43 ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:5000] "11738" "1054" "9775" "13838" ...
  ..$ : chr [1:5000] "11738" "1054" "9775" "13838" ...
 - attr(*, "method")= chr "DTW_BASIC"
 - attr(*, "call")= language proxy::dist(x = df, method = "dtw_basic", normalize = FALSE)

我想将其转换为类 dist 的新对象,以便新对象必须像

str(d_new)
 'dist' Named num [1:...] 1.67 2.1 1.44 1.62 1.68 ...

当然,在 R 中有一个函数或一个简单的方法可以做到这一点 虽然我搜索了很多,但我找不到它。感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

这个怎么样:

library(dtwclust)
set.seed(5301)
x <- matrix(rnorm(500), ncol=5)
d <- proxy::dist(x, method="dtw_basic", normalize=FALSE)
class(d)
# [1] "crossdist"

d2 <- stats::as.dist(d)
class(d2)
# [1] "dist"
str(d2)
# 'dist' num [1:4950] 8.9 5.47 9.57 6.85 6.62 ...
# - attr(*, "Size")= int 100
# - attr(*, "call")= language as.dist.default(m = d)
# - attr(*, "Diag")= logi FALSE
# - attr(*, "Upper")= logi FALSE