Computing net distance (Euclidean distance) in R

Related searches

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

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.

[PDF] Package 'distances', in R. It calculates distances only as needed (unlike the standard dist function which derives Mahalanobis distances or normalized Euclidean distances are both possible. If dist_variables is NULL, all columns will be used (net of eventual. The Euclidean distance is computed between the two numeric series using the following formula: D= √(xi −yi)2) The two series must have the same length. This distance is calculated with the help of the dist function of the proxy package.

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)

R as a ruler? How to calculate distances between geographical , Euclidean distance is a great tool to calculate the distance between Now that we have our basic grid, we need to create an igraph network. Computing euclidean distance. To compute Euclidean distance, you can use the R base dist() function, as follow: dist.eucl <- dist(df.scaled, method = "euclidean") Note that, allowed values for the option method include one of: “euclidean”, “maximum”, “manhattan”, “canberra”, “binary”, “minkowski”.

Is it possible to calculate Euclidean distance directly in the function?

E.g. with this function am trying to calculate distance up to 8 m between observations, but I want to add a condition of only calculating distances between observations from the same group? My observations are trees with x,y position coordinates from different plots (11 plots, 2000 trees).

`search_dfixed <- function(id, x, y, dist) {
.class <- if (class(id) == "factor") {
"character"
 } else {
 class(id)
 }

 m <- as.matrix (dist(data.frame(x, y)))

 rownames(m) <- id; colnames(m) <- id

 z <- as.data.frame(m) %>%
 tibble::as_tibble() %>%
 dplyr::mutate(id = id) %>%
 tidyr::gather(competitor, .dist, -id) %>%
 dplyr::filter(
  .dist != 0,
  .dist <= dist
  ) %>%
  dplyr::arrange(id, .dist) %>%
  dplyr::mutate(
  id = 'class<-'(id, .class),
  competitor = 'class<-'(competitor, .class)
  )

  return(z)
  }`

How can I speed up the Euclidian distance estimation in a raster , How to calculate the polygon area using R? Question. 14 answers. Asked 11th Dec, 2013. If your value is extreme (above 97.5 or below 2.5 percentile normally) you have a higher ( or lower ) r with your original data then expected by chance and conclude you have isolation by distance

Euclidean distance, In mathematics, the Euclidean distance or Euclidean metric is the "ordinary" straight-line polar coordinates of the point p are (r1, θ1) and those of q are (r2, θ2), then the distance between the points is In computer science (and especially computer graphics, simulations, and video game development), GameDev.net. In other words, the Gower distance between vectors x and y is simply mean(x!=y). In this situation, you can save a significant amount of computation time by avoiding computing the entire distance matrix, and instead using colSums. Here is an example, with three levels and 10000 training rows:

Euclidean distance weight function - MATLAB dist, This MATLAB function takes these inputs, S-by-R weight matrix R-by-Q matrix dist is also a layer distance function which can be used to find the distances between neurons in a layer. Here you define a random weight matrix W and input vector P and calculate To change a network so an input weight uses dist , set net. Calculate dissimilarity/distance metrics. This function calculates a variety of dissimilarity or distance metrics. Although it duplicates the functionality of dist() and bcdist(), it is written in such a way that new metrics can easily be added.

A popular choice of distance metric is the Euclidean distance, which is the square root of sum of squares of attribute differences. In particular, for two data points x and y with n numerical attributes, the Euclidean distance between them is: For example, the data frame mtcars consists of measurements from a collection of 32 automobiles. Since

Comments
  • Once you have a matrix with two columns (one for x and one for y coordinates), dist(mymatrix) should be all you need.
  • Karen, just use lapply( dfs , function(x) dist( x[,3:4] , diag = TRUE ) ) you don't really need diag = TRUE, but it helps to see that dist calculates zero distance between a point and itself!
  • @SimonO101 thanks once again! How do I put the data that it produces into a matrix on the workspace? Results just popped out into the console. Thanks!
  • @KarenGraceBondoc assign it to a variable!! i.e. results <- lapply(.... Note, that it will give you a list of matrices, one matrix for each unique track. To access a specific one use the [[ operator, i.e. if you assign the output to a variable called results, then results[[1]] will give you the distance matrix for the first track.
  • @SimonO101 what I mean is that it will be in the workspace as data (I am using R Studio). Thanks!
  • @Karen if you just want distance between first point and all others using this subsetting will be better. If you want the whole matrix for all the points use what I posted in the comments above.
  • by first point here, do you mean the first point of each track? Because I have to compute for each track independently. Thanks!
  • Yes, I mean first point of each track. That is why we are using lapply again. We previosuly split your data into a list of dataframes, one dataframe for each track. We now independently use dist on the dataframe for each track (remember we split your data using the split function in your previous question). What you get back is a list of the distances, for each track between the first point and all other points.
  • when I checked the data using the script that you gave, it sorts of recycle the results in the last data of each track. Thanks!
  • @KarenGraceBondoc no problem. You should try to work out how it works. Try running it on lapply( dfs[[1]] .... to see what happens. You should only get the results for the first track back... lapply is a convenient way to type: take this list of stuff (in your case a list of dataframes) and apply some function to each item in the list.