RNA
RNA

Reputation: 153581

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. the question is, is there any function that will give the distances of observation 5 to all the other observations.

> 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

Upvotes: 1

Views: 449

Answers (2)

cory
cory

Reputation: 6669

Check out the pdist package. It basically returns a single row of what as.matrix(dist(x)) would return... so you don't have to calculate everything.

http://cran.r-project.org/web/packages/pdist/index.html

Upvotes: 0

joran
joran

Reputation: 173677

Just convert it to a matrix:

as.matrix(b)[5,]

Upvotes: 0

Related Questions