While there are many similar topics (here and here, for example) I have a slightly different end goal than other questions I've seen on SO. For reference, I am using R v 3.1.0.
I have two matrices. Each contains coordinates for points. The first (A) contains 2,107,377 points, and the second (B) contains 26,577 points.
I want to find the point in B that each point in A is closest to. That is, I want to calculate the distance between point 1 in A and each point in B (26,577 distances), and store the minimum. I want to do this for every point in A (2,107,377 minima). The goal is to group the points in A together based on the point in B they are closest to. Thus, some points in B will not be assigned; while others (many) will be assigned to multiple points in A.
I have tried:
test = which.min(sapply(1:nrow(coordinates), function(i)
spDistsN1(matrix(A, ncol = 2), matrix(B[i,], ncol = 2),
longlat = TRUE)))
but ran into a memory allocation problem (could not allocate a >16 Mb vector).
I'm running a for loop now:
for (i in 1:nrow(A)) {
minimum[i] = which.min(spDistsN1(matrix(A, ncol = 2), matrix(B[i,], ncol = 2),
longlat = TRUE))
}
But this, I'm expecting, is going to lead to the same result, just more slowly.
I figured before I tried a totally different approach (perhaps learning the raster package), I would see if anyone has any ideas.