I am trying to compute some distances for each combination of lat/lon values I have.
The first data frame looks like:
lon lat NOMVIAL
<dbl> <dbl> <chr>
1 -99.1 19.5 Tepozanes
2 -99.0 19.3 Bartolomé Díaz de León
3 -99.2 19.3 Renato Leduc
4 -99.2 19.2 Cuautlalpan
The second data frame looks like:
CVEGEO mean_lat mean_lon
<int> <dbl> <dbl>
1 90130143 19.2 -99.1
2 90130234 19.2 -99.0
3 90090300 19.2 -99.0
So I want to take each combination in df2
and compute the distances for each row in df1
and storing the results as a matrix. I can compute the distances for a single lat/lon value using:
df1 %>%
add_column(
M_lat = -99.183203,
M_long = 19.506582
) %>%
mutate(
Distance = geosphere::distHaversine(cbind(lon, lat), cbind(M_lat, M_long))
)
However, I would like to use map
and store the results as a matrix, as below.
Expected output:
90020001 90030001 90040001 90040010 90040020
Tepozanes 999 111 ... ... ...
Renato Leduc
Samahil
...
Primera ... ... ... ... ...
Where the column names come from the column GVEGEO
column in the df2
and the row names come from the column NOMVIAL
in the df1
data frame. The values being the distances computed.
Data:
df1 <- structure(list(lon = c(-99.12587729, -99.03630014, -99.16578649,
-99.18373215, -99.21312146, -99.29082258, -99.19958018, -99.05745354,
-99.09046923, -99.04686154), lat = c(19.543991, 19.2921902, 19.29272965,
19.2346386, 19.29264198, 19.32628302, 19.29913009, 19.38650317,
19.47120537, 19.31618134), NOMVIAL = c("Tepozanes", "Bartolomé Díaz de León",
"Renato Leduc", "Cuautlalpan", "Samahil", "Ninguno", "Monte de Sueve",
"Ninguno", "Rinoceronte", "Primera")), row.names = c(NA, -10L
), class = c("tbl_df", "tbl", "data.frame"))
df2 <- structure(list(CVEGEO = c(90130143L, 90130234L, 90090300L, 90130284L,
90130290L), mean_lat = c(19.2256141377143, 19.2447500775758,
19.209320585524, 19.2219817711111, 19.2405991752941), mean_lon = c(-99.143825052,
-99.0409973439394, -98.9713545799563, -99.1172106433333, -99.1347260164706
)), row.names = c(NA, -5L), class = c("tbl_df", "tbl", "data.frame"
))
EDIT:
To clarify, hopefully, a little more clearly.
I would like to take each row of df2
and use this to compute the distances of all rows in df1
.
Take row 1 in df2
CVEGEO mean_lat mean_lon
90130143 19.22561 -99.14383
Take these values and compute the distance for all of the rows in df1
- so for each of the 10 rows in df1
I will have a distance computed based on this row in df2
. Then move to row 2 of df2
and do the same again...
So this matrix will have dimensions 5 columns and 10 rows.