我不明白dist()函数是如何计算两个向量之间的欧几里得距离的。我创建了最简单的一维向量,我希望得到一个结果的解释。下面是一个名为test.csv的CSV文件形式的向量:
Item Name Attribute
Item 1 3
Item 2 5代码如下:
test <- read.csv("test.csv", header = TRUE)
dist(test, method = "euclidean")下面是我得到的输出:
dist(test, method = "euclidean")
1 2 2.828427
Warning message: In dist(test, method = "euclidean") : NAs introduced by coercionR的版本是: Rx64 3.4.3
我希望结果是2,而不是2.828427,因为我假设欧几里德距离是通过公式d = sqrt((xi - yi)^2)计算的。
将test.csv文件中的数字代入我得到的公式中:
d = sqrt((3-5)^2) = 2发布于 2018-01-07 00:42:43
当您传递矩阵时,它会将您的输入视为n乘以m维数向量,请参见:
dist(t(df[,c("Name","Attribute")])) # distance between the two columns
dist(df[,c("Name","Attribute")]) # distance between the two rows
sqrt((1-3)^2 + (2-5)^2) # distance between the two columns
sqrt((1-2)^2 + (3-5)^2) # distance between the two rows我认为这就是你想要的:
apply(df[,c("Name","Attribute")],1, function(x) {abs(x[1]-x[2])})https://stackoverflow.com/questions/48129316
复制相似问题