I am doubtful that the problem is the sheer number of combinations, since 600 points has only 360,000 possible combinations, which would fit on most machines. Rather, I think your issue is the dimensions matrix that dist gives, since one row isn't necessarily equal to one column in terms of memory space.
Therefore, I think a better bet would might be to generate all of the pairs first - then you are in control of the dimensions of the resulting matrix. Here are a couple different ideas for how you might approach getting to the mean:
1. Generate all possible combinations of points, calculate the distance between them, and take the average of all of those distances:
library(dplyr)
library(tidyr)
coords <- lapply(
1:600,
function(x) {
data.frame(x = rnorm(1), y = rnorm(1))
}
)
combos <- expand.grid(
coords,
coords
) %>%
unnest(everything(), names_repair = 'unique') %>%
rename(
'x1' = 1,
'y1' = 2,
'x2' = 3,
'y2' = 4
)
#> New names:
#> * x -> x...1
#> * y -> y...2
#> * x -> x...3
#> * y -> y...4
mean_dist <- combos %>%
mutate(
distance = sqrt((x2 - x1)^2 + (y2 - y1)^2)
) %>%
pull(distance) %>%
mean
mean_dist
#> [1] 1.833072
Created on 2022-05-05 by the reprex package (v1.0.0)
2. Create the numerator and denominator elementwise without storing the combinations
One other option would be to break the problem down into two pieces: How to generate the combinations, and how to calculate the distance. Once you solve those two problems, finding the mean is really trivial. Working backwards, we realize that a mean is just \dfrac{sum}{n}, where sum is the sum of the distances calculated and n is the number of distances calculated. So if we are really memory constrained in solving the problem, we just won't store all of the combinations in memory and instead only increment sum and n for each combination. Here is an example of that:
numerator <- 0
denominator <- 0
for(coord1 in coords) {
for(coord2 in coords) {
distance <- sqrt((coord2$x - coord1$x)^2 + (coord2$y - coord1$y)^2)
numerator <- numerator + distance
denominator <- denominator + 1
}
}
numerator / denominator
#> [1] 1.833072