All Questions

Tagged with
Filter by
Sorted by
Tagged with
10 votes
4 answers
56k views

How to calculate Euclidian distance between two points defined by matrix containing x, y?

I am very lost in Euclidean distance calculation. I have found functions dist2{SpatialTools} or rdist{fields} to do this, but they doesn´t work as expected. I suppose that one point has two ...
maycca's user avatar
  • 3,980
8 votes
2 answers
8k views

calculating the euclidean dist between each row of a dataframe with all other rows in another dataframe

I need to generate a dataframe with minimum euclidean distance between each row of a dataframe and all other rows of another dataframe.Both my dataframes are large (approx 40,000 rows).This is what I ...
user14845's user avatar
  • 105
6 votes
2 answers
9k views

Creating a distance matrix from a list of coordinates in R

I have a csv file with a list of co-ordinate positions for over 2000 farms, with the following structure; FarmID | Latidue | Longitude | ------ |---------|-----------| 1 | y1 | x1 ...
sruby52's user avatar
  • 83
6 votes
2 answers
5k views

R - How to get row & column subscripts of matched elements from a distance matrix

I have an integer vector vec1 and I am generating a distant matrix using dist function. I want to get the coordinates (row and column) of element of certain value in the distance matrix. Essentially I ...
DKangeyan's user avatar
  • 533
6 votes
1 answer
966 views

trying to draw circles based on distance between points

I'm trying to draw some circles and I was sort of hoping they would intersect with some points, alas... library(maptools) library(plotrix) xy <- matrix(runif(20, min = -100, max = 100), ncol = 2) ...
Roman Luštrik's user avatar
6 votes
1 answer
6k views

Weighted Euclidean Distance in R

I'd like to create a distance-matrix with weighted euclidean distances from a data frame. The weights will be defined in a vector. Here's an example: library("cluster") a <- c(1,2,3,4,5) b <- ...
h7681's user avatar
  • 355
6 votes
2 answers
95 views

Efficiently accessing pairwise distances

I have a distance matrix: > mat hydrogen helium lithium beryllium boron hydrogen 0.000000 2.065564 3.940308 2.647510 2.671674 helium 2.065564 0.000000 2.365661 1.697749 1....
Dex's user avatar
  • 63
5 votes
2 answers
4k views

Distance matrix from two separate data frames

I'd like to create a matrix which contains the euclidean distances of the rows from one data frame versus the rows from another. For example, say I have the following data frames: a <- c(1,2,3,4,5)...
h7681's user avatar
  • 355
5 votes
1 answer
3k views

Finding euclidean distance in R{spatstat} between points, confined by an irregular polygon window

I'm trying to find the euclidean distance between two points, confined by an irregular polygon. (ie. the distance would have to be calculated as a route through the window given) Here is an ...
user3389288's user avatar
  • 1,012
5 votes
1 answer
2k views

What is the complexity of dist()?

I used the dist function in R and I am wondering the time complexity of it. I know that the hierarchical clustering has a N^2*logN time complexity. And hierarchical clustering is composed of two ...
sclee1's user avatar
  • 1,217
5 votes
2 answers
3k views

r distance between rows

I apologize this is my attempt at redeeming myself after a disastrous earlier attempt . Now I have a bit more clarity. So here I go again. My goal is to find rows that are similar. So first I am ...
Emily Fassbender's user avatar
4 votes
4 answers
1k views

(Speed Challenge) Any faster method to calculate distance matrix between rows of two matrices, in terms of Euclidean distance?

First of all, this is NOT the problem of calculating Euclidean distance between two matrices. Assuming I have two matrices x and y, e.g., set.seed(1) x <- matrix(rnorm(15), ncol=5) y <- matrix(...
ThomasIsCoding's user avatar
4 votes
1 answer
131 views

Wrong Euclidean distance H2O calculations R

I am using H2O with R to calculate the euclidean distance between 2 data.frames: set.seed(121) #create the data df1<-data.frame(matrix(rnorm(1000),ncol=10)) df2<-data.frame(matrix(rnorm(300),...
Jesus's user avatar
  • 462
3 votes
4 answers
881 views

Pairwise weighted distance vectorization

The following efficient and vectorized Matlab code computes the Weighted Euclidean Distance between 2 sets of points A and B using a weight vector WTS (1 weight for each dimension; same weights for ...
rcpinto's user avatar
  • 4,213
3 votes
2 answers
956 views

Computing Euclidean distances between subsequent positions

I have large data frames consisting of pairs of X and Y coordinates, and wish to calculate the Euclidean distances between consecutive coordinates (minimal size is around 2000 pairs of coordinates). ...
rebeccmeister's user avatar
3 votes
1 answer
6k views

Distance between two sets of points [duplicate]

So after looking at various Questions asked here on stackoverflow I'm still not able to wrap my head around the dist function in R or maybe even a distance matrix in general. So I have two dataframes ...
the_man_in_black's user avatar
3 votes
2 answers
430 views

R: calculate Euclidean distance between two raster layers pixels

I have two raster layers for same area. I need to find the Euclidean distance between cell of coarse resolution raster and cell of fine resolution raster that fall within each cell of the pixels from ...
Nikos's user avatar
  • 440
3 votes
1 answer
108 views

How can I generate n points with euclidan distance less than d from X

Let's say I have a point: point <- c(1, 2) How can I generate 10 points with euclidan distance from point lower than some choosen value, e.g. 3. In other words: how can I generate set of points in ...
SathOkh's user avatar
  • 826
3 votes
3 answers
2k views

euclidean distance between vectors grouped by other variable in SPSS, R or Excel

I have a dataset containing something like this: case,group,val1,val2,val3,val4 1,1,3,5,6,8 2,1,2,7,5,4 3,2,1,3,6,8 4,2,5,4,3,7 5,1,8,6,5,3 I'm trying to compute programmatically the Euclidean ...
lorenzov's user avatar
3 votes
2 answers
978 views

R, compute the smallest Euclidean Distance for two dataset, and label it automatically

I'm working with Euclidean Distance with a pair of dataset. First of all, my data. centers <- data.frame(x_ce = c(300,180,450,500), y_ce = c(23,15,10,20), ...
s__'s user avatar
  • 9,395
3 votes
2 answers
3k views

5 nearest neighbors based on given distance in r

I have the following dataset: id x y age 1 1745353 930284.1 30 2 1745317 930343.4 23 3 1745201 930433.9 10 4 1745351 930309.4 5 5 1745342 930335.2 2 6 ...
user2797174's user avatar
3 votes
1 answer
3k views

Calculate euclidean distance in a faster way

I want to calculate the euclidean distances between rows of a dataframe with 30.000 observations. A simple way to do this is the dist function (e.g., dist(data)). However, since my dataframe is large, ...
Joachim Schork's user avatar
3 votes
1 answer
947 views

How to use apply function to calculate the distance between two matrices

I'm trying to calculate the euclidean distance between two matrices. I have already achieved that using 2 for loops but trying to vectorize the calculation to speed up. I'm using pdist as a benchmark ...
Ray's user avatar
  • 59
3 votes
2 answers
522 views

How to pull points that are within a certain distance away in R?

I have a parameter space given by (x,y) with x values from 1:5 and y values from 1:8. Let's say my current point p is located at (2,5) (it is colored in red). My goal is to try to pull all the points ...
road_to_quantdom's user avatar
3 votes
1 answer
2k views

Euclidean distance in R using two variables in a matrix

I am quite new to R and I am trying to compute the gross distance (or the sum of the Euclidean distance on all data points) from two variables in my matrix and net distance (Euclidean distance between ...
Kaye11's user avatar
  • 359
2 votes
3 answers
2k views

How to calculate Euclidean distance in R for n-dimensions?

I'm trying to write a function which calculates Euclidean distance between two points across n-dimensions. I have the following code: euc_dist <- function(x1, x2) sqrt(sum((x1 - x2) ^ 2)) This ...
HelpNeeded3's user avatar
2 votes
1 answer
4k views

Distance between two 3D points in R

I have two 2 points A(x,y,z) e B(x1,y1,z1). I want to calculate the euclidean distance between these point at time1, time2, time3. So, I have a matrix like this: x y z x1 y1 ...
piravi's user avatar
  • 109
2 votes
5 answers
3k views

Calculate Euclidean distances between all rows in matrices A and B

I have two matrices, A and B, with N_a and N_b rows, respectively. I need to calculate the Euclidean distance between all pairwise combinations of an element in A (a) and another in B (b), such that ...
Henry David Thorough's user avatar
2 votes
2 answers
1k views

R User-defined function works alone but returns incorrect values when used with apply

The user defined function (dist.func) runs and provides the correct output when I use it on a single line of data but does not provide the correct output (still executes) when I embed it into the ...
David Roberts's user avatar
2 votes
2 answers
344 views

How to improve processing time for euclidean distance calculation

I'm trying to calculate the weighted euclidean distance (squared) between twoo data frames that have the same number of columns (variables) and different number of rows (observations). The ...
Ana F.'s user avatar
  • 43
2 votes
2 answers
214 views

Automating (loops) Euclidean distance measurements in R

AIM: I want to automate (loop) the code below, without having to manually run it for each sample. I have a terrible habit of writing long-hand in base, and need to start using loops, which I find ...
Rchaeologist's user avatar
2 votes
2 answers
95 views

Loop on a data frame with considering the types of the values

I have a large data set of coordinates and I want to calculate the Euclidean distance among them. The sample of that would be like this: df <- data.frame("name" = c("a","b","c","a","e"), "type" = ...
minoo's user avatar
  • 565
2 votes
1 answer
1k views

R: results differ when calculating Euclidean distance between two vectors with different methods

Suppose that I have two vectors. x1 = c(-1, 2, 3) x2 = c(4, 0, -3) To calculate the Euclidean distance, I used three different ways 1- The built function norm s = cbind(x1, x2) norm(s, "2") #[1] 5....
jeza's user avatar
  • 299
2 votes
3 answers
82 views

Euclidean distant for NON-CONSECUTIVE classes of factors iterated by groups

This question is an extension of this question. Euclidean distant for distinct classes of factors iterated by groups The same explanations from the previous question apply here as well. I want to ...
lovestacksflow's user avatar
2 votes
1 answer
448 views

Calculate Euclidean distance between points with rolling function in data.table

I have a data.table with transects in different areas, coordinates x and y are projected as UTM 36S. How can I calculate the (Euclidean) distance between consecutive points with data.table, per area, ...
Henk's user avatar
  • 3,656
2 votes
1 answer
83 views

Permutation of euclidean distances between 13 row indexes in R

I have 13 Area of Interests (AOIs) with x and y values for each test image. How can I get all possible combination of euclidian distance between pairs of AOIs, ((x-xi)+(y-yi))^(1/2)? Ultimately I am ...
Hyokstayo's user avatar
2 votes
1 answer
403 views

How to select n objects from a set of N objects, maximizing the sum of pairwise distances between them

You have a set of N=400 objects, each having its own coordinates in a, say, 19-dimensional space. You calculate the (Euclidean) distance matrix (all pairwise distances). Now you want to select n=50 ...
user6376297's user avatar
2 votes
1 answer
46 views

Distance matrix to reshaped vector of distances sorted by positions

I have a data frame of 5 items, as follows: df = structure(list(item1 = c(1, 1, 1, 1, 2, 2, 2, 3, 3, 4), item2 = c(0, 2, 3, 4, 0, 3, 4, 0, 4, 0)), row.names = c(NA, -10L), class = c("tbl_df", "tbl",...
Omry Atia's user avatar
  • 2,443
2 votes
1 answer
898 views

Compute the Euclidean distances between corresponding rows of 2 matrices

I have 2 matrices A and B, and I want to compute the Euclidean distances between 1st row of A and 1st row of B, 2nd row of A and 2nd row of B, etc. I know I can use rdist function to compute the ...
user5054's user avatar
  • 1,076
2 votes
4 answers
5k views

Find shortest distance between multiple points

Imagine a small dataset of xy coordinates. These points are grouped by a variable called indexR, there are 3 groups in total. All xy coordinates are in the same units. The data looks approximately ...
Quinn's user avatar
  • 419
2 votes
1 answer
2k views

Calculate Rao's quadratic entropy

Rao QE is a weighted Euclidian distance matrix. I have the vectors for the elements of the d_ijs in a data table dt, one column per element (say there are x of them). p is the final column. nrow = S. ...
JerryN's user avatar
  • 2,456
2 votes
2 answers
1k views

Find which points are within a given distance of each point

I have a dataframe that has a list of places and their geographical locations in UTM (Universal Transverse Mercator) coordinates. It looks something like this: Place X_UTM Y_UTM 1 574262.0 ...
Leandro Zipitria's user avatar
2 votes
1 answer
50 views

Calculate distance with multiple starting coordinates

I calculated the travelled distance called distance_m per date with the function points_in_circle for two ID's called HEX_Tag_ID. The coordinates that I put in the function are based on the Datum &...
Pepijn95's user avatar
  • 321
2 votes
1 answer
438 views

Calculating two dimensions Euclidean distance and adding it as a column in the data

I need to create a variable that measures distance from APP. The center of APP is at the coordinates 1440000 east, 12160000 north. I need to make a new column calculating the euclidean distance ...
Alison Meeth's user avatar
2 votes
1 answer
148 views

Euclidean distant for distinct classes of factors iterated by groups

*Update: The answer suggested by Rui is great and works as it should. However, when I run it on about 7 million observations (my actual dataset), R gets stuck in a computational block (I'm using a ...
lovestacksflow's user avatar
2 votes
1 answer
172 views

Calculate distances between a line and all points on an intersecting plane in r

I have a regular, rectangular grid of x, y, z values (an image). I also have an x, y, z line that intersects the grid. For each plane (z-level) in the grid, I want to compute the Euclidean distance ...
Brian D's user avatar
  • 2,650
2 votes
1 answer
205 views

What is dist() actually computing?

I have a data frame, tmp: class x y 1 A -2.8959969 -0.3192259 2 B -0.2401775 0.5801373 I compute dist(tmp, method="euclidean", diag=TRUE, upper=FALSE, p=2) which yields : ...
irritable_phd_syndrome's user avatar
2 votes
1 answer
2k views

Get maximum distance between points in a vector (R)

I have two vectors of latitudes and longitudes. I would like to find the maximum distance between the points. The way I see it, I should get a matrix of distances between all points and get the max of ...
Anarcho-Chossid's user avatar
2 votes
1 answer
192 views

Unique combinations by group

I have the following data frame structured in terms of 3 variables, i.e Location, Latitude, and Longitude within every single group. I would like to calculate the euclidean distance between all unique ...
9834's user avatar
  • 21
2 votes
0 answers
154 views

Interactive visualization of Euclidean distance with R

I would like to know if there is any way to visualize in an interactive way the process of euclidean distance with R. In a few words to recreate the plot below with a package that permits interactive ...
firmo23's user avatar
  • 8,034