I need to generate a distance matrix with the geographic (Euclidian) distances between 48 points sampled in a regular grid with exactly 120 centimeters between neighbouring points both vertically and horizontally:
Thus, the distance between sample 1K (cell A1) and both 1NP(cell A2) and 2WN(cell B1) is 120 cm, and 240 cm for 1K-1WNP and 1K-3W, and so on. Diagonally, the distances can be calculated using simple Pythagoras, so that the distance from 1K (A1) to 2WP (B2) is Squareroot(120^2 + 120^2)=169,7, and that from 1K(A1) to 2P(B3) is Squareroot(240^2 + 120^2)= 268,33, and so on.
This will make for a total matrix for distances between all points of 48x48. Here are the first two columns and 16 rows of how the matrix should look:
How does one do that in R? I have tried to define a set of data and use the dist() function, but that clearly does not work.


Use {sf} package:
st_make_gridandst_distancefunctions like:Created on 2024-02-24 with reprex v2.1.0
Please note the distances are in meters, you have to convert it to centimeters if required, for example: