Computing net distance (Euclidean distance) in R

江枫思渺然 提交于 2019-12-04 18:30:46

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.

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