r - 将交叉距离矩阵转换为距离矩阵
问题描述
我有一个使用包获得的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 中有一个函数或一个简单的方法可以做到这一点,但我找不到它,尽管我搜索了很多。谢谢你的帮助。
解决方案
这个怎么样:
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
推荐阅读
- mariadb - MariaDB我们可以创建一个函数索引(基于函数的索引)
- wordpress - Wordpress - 如何在 wordpress 上编辑过滤器
- javascript - 如何理解从 MediaRecorder 获得的音频元数据?
- python - 计算季度结束日期
- typescript - 使用 TypeScript 的方法重载不会推断参数类型
- android - 如何在android中的两个视频流之间添加GL转换?
- r - 如何检索表的列/字段名称?
- swift - Swift,协议条件一致性,Element: Protocol 和 Element == Protocol 在实现嵌套数组时有什么区别?
- python - Cameo Systems Modeller 和 Python 之间的连接
- python - pipenv shell 不遵循它的起源路径