[英]how to ge the distances of one observation to all the others from a dist object?
Suppose I have a data.frame a
(each observation is in a row) and calculated the distance matrix. 假设我有一个data.frame a
(每个观察结果都在一行中)并计算了距离矩阵。 the question is, is there any function that will give the distances of observation 5 to all the other observations. 问题是,是否有任何函数可以将观测值5的距离赋予所有其他观测值。
> a=data.frame(A=rnorm(10), B=rnorm(10), C=rnorm(10))
> b=dist(a)
> b
1 2 3 4 5 6 7 8 9
2 1.6118634
3 0.4891468 1.3382692
4 1.2002947 1.7516061 0.9160975
5 1.8128570 0.3197837 1.5192406 1.7709168
6 0.7280433 1.2628696 0.4063128 1.2411639 1.4971098
7 1.7616767 0.7400666 1.4512844 1.4355922 0.5168996 1.5524980
8 3.1033274 3.3739578 2.7297046 2.2281075 3.3693333 2.7738859 3.1216145
9 2.0916857 1.6749526 1.6717408 2.0293415 1.8196557 1.3704288 1.9824870 2.4013682
10 1.5949320 1.7309838 1.1680365 0.6331770 1.7255615 1.3234977 1.4333926 1.7798153 1.6126823
只需将其转换为矩阵:
as.matrix(b)[5,]
Check out the pdist package. 签出pdist包。 It basically returns a single row of what as.matrix(dist(x)) would return... so you don't have to calculate everything. 它基本上返回as.matrix(dist(x))将返回的一行...因此您不必计算所有内容。
http://cran.r-project.org/web/packages/pdist/index.html http://cran.r-project.org/web/packages/pdist/index.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.