two - symmetric distance matrix r
how to calculate all pairwise distances in two dimensions (2)
The aptly named
dist() will do this:
x <- matrix(rnorm(100), nrow=5) dist(x) 1 2 3 4 2 7.734978 3 7.823720 5.376545 4 8.665365 5.429437 5.971924 5 7.105536 5.922752 5.134960 6.677726
?dist for more details
Say I have data concerning the position of animals on a 2d plane (as determined by video monitoring from a camera directly overhead). For example a matrix with 15 rows (1 for each animal) and 2 columns (x position and y position)
animal.ids<-letters[1:15] xpos<-runif(15) # x coordinates ypos<-runif(15) # y coordinates raw.data.t1<-data.frame(xpos, ypos) rownames(raw.data.t1) = animal.ids
I want to calculate all the pairwise distances between animals. That is, get the distance from animal a (row 1) to the animal in row 2, row3...row15, and then repeat that step for all rows, avoiding redundant distance calculations. The desire output of a function that does this would be the mean of all the pairwise distances. I should clarify that I mean the simple linear distance, from the formula d<-sqrt(((x1-x2)^2)+((y1-y2)^2)). Any help would be greatly appreciated.
Furthermore, how could this be extended to a similar matrix with an arbitrarily large even number of columns (every two columns representing x and y positions at a given time point). The goal here would be to calculate mean pairwise distances for every two columns and output a table with each time point and its corresponding mean pairwise distance. Here is an example of the data structure with 3 time points:
xpos1<-runif(15) ypos1<-runif(15) xpos2<-runif(15) ypos2<-runif(15) xpos3<-runif(15) ypos3<-runif(15) pos.data<-cbind(xpos1, ypos1, xpos2, ypos2, xpos3, ypos3) rownames(pos.data) = letters[1:15]
Why do you compare d<-sqrt(((x1-x2)^2)+((y1-y2)^2))?
Do d^2<-(((x1-x2)^2)+((y1-y2)^2)). It will cost you much less.