Computing net distance (Euclidean distance) in R

2019-04-14 23:22发布

问题:

I have asked about and receive great help for computing Euclidean distance in R before. Now, I need to compute the Euclidean distance from the first point relative to all the other points within the track data. Here is how my data looks like:

dput(head(t1))
structure(list(A = c(0L, 0L, 0L, 0L, 0L, 0L), T = 0:5, X = c(668L, 
668L, 668L, 668L, 668L, 668L), Y = c(259L, 259L, 259L, 259L, 
259L, 259L), V = c(NA, 0, 0, 0, 0, 0)), .Names = c("A", "T", 
"X", "Y", "V"), row.names = c(NA, 6L), class = "data.frame")

And SimonO101 was so great in giving me a code that will compute the Euclidean distance from the starting position to the final position for each track:

## Split the data
dfs <- split(t1,t1$A)

## Find hypotenuse between first and last rows for each A
lapply( dfs , function(x){
  j <- nrow(x)
  str <- x[1,c("X","Y")]
  end <- x[j,c("X","Y")]
  dist <- sqrt( sum( (end - str)^2 ) )
  return( dist )
} )

How do I edit the code, so that it will not just have the Euclidean distance from start to end, but from every X,Y position? Thanks again!

EDIT: And also: How to visualize the results as a matrix. Thank you

回答1:

I would use the dist function in the stats package. You can apply it to your data easily enough:

lapply( dfs , function(x) dist( x[,c("X","Y")] , diag = TRUE )[1:nrow(x)] )

The idea being that we operate on each dataframe, applying the dist function to the "X" and "Y" columns of each data frame. The subsetting at the end ( [1:nrow(x)] )is used to return only the distances between the first point and all the other points. Remove this subsetting if you want a full distance matrix for each track.



回答2:

A possible solution :

f <- function(x) {
j <- nrow(x)
end <- as.numeric(x[j,c("X","Y")])
x <- x[-j,c("X", "Y")]
dist <- colSums((t(x)-end)^2)
dist
}
lapply(dfs, f)