# [R] dist() between groups of data

Vayssières, Marc marcv at water.ca.gov
Fri Feb 24 18:23:36 CET 2006

```Henrik,

If I understand your question, what you want is:

x <- runif(9)
y <- runif(9)
z <- rep(c(0,1,2), each=3)
xyz <- cbind(x,y,z)
xyz
d.all <- dist(xyz[,c("x","y")])
d.all
xyz.02 <- subset(xyz, z == 0 | z == 2)
xyz.02
d.02 <- dist(xyz.02[,c("x","y")])
d.02

Marc

-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Henrik Parn
Sent: Thursday, February 23, 2006 8:40 AM
To: r-help at stat.math.ethz.ch
Subject: [R] dist() between groups of data

Dear all,

I have a data set with x and y positions of nests. Each nest can be grouped according to a factor z. I have made a small dummy data set, where the grouping variable z is 0 or 1:

x <- runif(6)
y <- runif(6)
z <- rep(c(0,1), each=3)
xyz <- cbind(x,y,z)
xyz
x           y z
[1,] 0.7176801 0.760944688 0
[2,] 0.2377928 0.080622524 0
[3,] 0.9450770 0.022039470 0
[4,] 0.8725492 0.971996677 1
[5,] 0.6356198 0.001569859 1
[6,] 0.8949670 0.066044377 1

First of all, I suppose that this is the correct way to calculate the Euclidean distance between /all/ nests:

xy <- cbind(x,y)
d.all <- dist(xy)

However, I would like to obtain a distance matrix for distances between nests belonging to certain groups. For example "calculate all distances from nests where z equals something, to nests where z equals something else".

Is it best to split the data in two matrices according to the value of z in the two groups to be compared, and then try to follow the suggestions by Gabor Grothendieck* Date:* Wed 07 Jan 2004 - 15:16:37 EST , or are there other ways that can be applied straight to one matrix?

Henrik

--
************************
Henrik Pärn
Department of Biology
NTNU
7491 Trondheim
Norway

+47 735 96282 (office)
+47 909 89 255 (mobile)
+47 735 96100 (fax)

______________________________________________
R-help at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help