简体   繁体   English

如何确定一个观测对象与dist对象之间的距离?

[英]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.

相关问题 一个因素与另一个因素之间的比较 - Comparison between one observation and the others by level of a factor 将数据帧转换为“dist”类的对象,而无需实际计算 R 中的距离 - Convert a dataframe to an object of class "dist" without actually calculating distances in R 如何通过在 R 中的大型数据框中观察来获得前 n 个距离? - How to get top n distances by observation in a large dataframe in R? 如何使用dist()函数计算组内的成对距离 - How to use dist() function to calculate pairwise distances within groups 如何从一个向量中收集观测值,并以另一个向量的值为条件 - How to collect observation from one vector, conditioned to the value of another one 如何从Delaunay三角剖分到R中列出对象的距离提取 - How to extract distances from delaunay triangulation to list object in R 如何从rpart对象获取新观察的终端节点? - How to get terminal nodes for a new observation from an rpart object? 如何使用 R 在合并中的两个观察中使用来自一个观察的数据? - How to use data from one observation in two observations in a merge, with R? 距离已知时如何计算每次观测的概率值 -> kmeans R 编程 - How to calculate the probability value for each observation when distances are known - > kmeans R programming 如何从r中的另一行中减去一行? - How to subtract one row from others in r?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM