[R-sig-Geo] inter-point distance calcs

Jim Bouldin jrbouldin at ucdavis.edu
Tue Aug 11 23:56:47 CEST 2009


Thanks much Kami, that works.  If one changed that last line of yours to:

data = subset(data, X>Y)
 
that would eliminate both the duplicates and the diagonal, leaving just one
instance of each distance.
Jim

> Maybe a little too complicated, but I had it at hands:
> 
> d <- data.frame(x=sample(1:10, 5), y=sample(1:10, 5)) #create a sample
> 
> data set
> ss2 <- as.data.frame(as.matrix(dist(d))) #create a data.frame to
> extract 
> row and column names
> X <- rep(seq(1:length(row.names(ss2))), length(names(ss2))) #make a 
> vector containing the X coordinate names
> Y <- rep(seq(1:length(names(ss2))), length(row.names(ss2))) #the same
> for Y
> Y <- sort(Y) #first sort
> coords <- cbind(X, Y) #then cbind
> data <- as.data.frame(cbind(coords, as.vector(as.matrix(dist(d))))) # 
> add the data
> data <- data[data$V3 >0,] #remove the distance to self
> 
> 
> hope this solves it.
> 
> Kami
> 
> 
> Jim Bouldin wrote:
> > Last message got truncated somehow.  To repeat, I forgot to mention that
> I
> > need to get the distances into the following format.  Using Dylan's
> example
> > above:
> >
> > Point Pair      Distance
> > 1-2             3.6 
> > 1-3             6.4
> > 1-4             9.2
> > .               .
> > .               .
> > 3-4             .
> > 3-4             .
> > 4-5             3.2
> >
> > That is, I need to be able to create a column vector that labels each
> pair
> > of points, which I can then cbin with the distance vector into a new
> matrix
> > (I know how to create the distance vector, but not the identifier
> vector).
> >  The point pair identifiers (labels) will come from a column in the
> > original data matrix.
> > Thanks
> >
> > _______________________________________________
> > R-sig-Geo mailing list
> > R-sig-Geo at stat.math.ethz.ch
> > https://stat.ethz.ch/mailman/listinfo/r-sig-geo
> >
> >
> >   
> 

Jim Bouldin, PhD
Research Ecologist
Department of Plant Sciences, UC Davis
Davis CA, 95616
530-554-1740



More information about the R-sig-Geo mailing list