Computing net distance (Euclidean distance) in R

元气小坏坏 提交于 2019-12-06 12:14:43

问题


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)


来源:https://stackoverflow.com/questions/16192483/computing-net-distance-euclidean-distance-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!