【发布时间】:2026-01-04 10:05:02
【问题描述】:
可能重复:
Convert a dataframe to an object of class “dist” without actually calculating distances in R
我有一个非常大的 csv 文件(因此 R 中的 for 循环需要很长时间)关键字之间的相似性,当我读入 data.frame 时看起来像这样:
> df
kwd1 kwd2 similarity
a b 1
b a 1
c a 2
a c 2
我想把它转换成一个 dist 对象,像这样:
> dObject
a b
b 1
c 2 0
我无法让它工作: Convert a dataframe to an object of class "dist" without actually calculating distances in R
我的另一个想法是使用 Matrix() 创建一个稀疏矩阵,但我不确定如何有效地填充矩阵,因为我的 csv 相当大 - 可能是一个应用函数?
也许 reshape()?
---- 更新---- 这似乎适用于上面的玩具数据集: https://stats.stackexchange.com/questions/6827/efficient-way-to-populate-matrix-in-r
然而,在这个例子中,他们使用了一个 matrix(),但我想使用 Matrix(),因为内存的原因是稀疏的。
--- 此外---- 之前有一个类似的帖子。但是,我认为它的建议不适用于这种情况,在这种情况下,它们不是数据集中每个元素之间的链接——csv 不包含所有关键字之间的成对相似性,如上一篇文章所示: Convert a dataframe to an object of class "dist" without actually calculating distances in R
【问题讨论】:
-
你试过什么没用?我认为我们应该等待关闭作为重复,直到 OP 有时间详细说明。