映射 lat/lon 值的每个组合并将距离存储在矩阵中
map over every combination of lat/lon values and store in a matrix the distance
我正在尝试为我拥有的 lat/lon 个值的每个组合计算一些距离。
第一个数据框如下所示:
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
第二个数据框如下所示:
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
所以我想采用 df2
中的每个组合并计算 df1
中每一行的距离并将结果存储为矩阵。我可以使用以下方法计算单个 lat/lon 值的距离:
df1 %>%
add_column(
M_lat = -99.183203,
M_long = 19.506582
) %>%
mutate(
Distance = geosphere::distHaversine(cbind(lon, lat), cbind(M_lat, M_long))
)
不过,我想使用 map
并将结果存储为矩阵,如下所示。
预期输出:
90020001 90030001 90040001 90040010 90040020
Tepozanes 999 111 ... ... ...
Renato Leduc
Samahil
...
Primera ... ... ... ... ...
其中列名来自 df2
中的 GVEGEO
列,行名来自 df1
数据框中的 NOMVIAL
列。这些值是计算的距离。
数据:
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"
))
编辑:
澄清一下,希望更清楚一点。
我想获取 df2
的每一行并用它来计算 df1
中所有行的距离。
取 df2
中的第 1 行
CVEGEO mean_lat mean_lon
90130143 19.22561 -99.14383
获取这些值并计算 df1
中所有行的距离 - 因此对于 df1
中的 10 行中的每一行,我将根据 [=] 中的这一行计算距离17=]。然后移动到 df2
的第 2 行并再次执行相同的操作...
所以这个矩阵的维度是 5 列和 10 行。
您可以使用以下解决方案。您必须交换第二个数据框中的 mean_lon
和 mean_lat
列,因为在交换 lon
和 lat
时会出现错误。
library(dplyr)
library(purrr)
library(tidyr)
library(tibble)
map2(df4$lon, df4$lat, ~ df5 %>%
rowwise() %>%
mutate(output = geosphere::distHaversine(c(.x, .y), c_across(mean_lon:mean_lat)))) %>%
set_names(df4$NOMVIAL) %>%
map(~ .x %>%
select(CVEGEO, output) %>%
pivot_wider(names_from = CVEGEO, values_from = output)) %>%
bind_rows() %>%
rownames_to_column() %>%
mutate(rowname = df4$NOMVIAL)
# A tibble: 10 x 6
rowname `90130143` `90130234` `90090300` `90130284` `90130290`
<chr> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Tepozanes 35492. 34483. 40636. 35857. 33786.
2 Bartolomé Díaz de León 13513. 5304. 11476. 11549. 11831.
3 Renato Leduc 7820. 14159. 22444. 9385. 6658.
4 Cuautlalpan 4313. 15044. 22501. 7133. 5193.
5 Samahil 10426. 18857. 27048. 12785. 10071.
6 Ninguno 19083. 27775. 36007. 21625. 18974.
7 Monte de Sueve 10065. 17730. 25985. 12194. 9429.
8 Ninguno 20078. 15874. 21699. 19361. 18158.
9 Rinoceronte 27908. 25739. 31724. 27885. 26088.
10 Primera 14334. 7976. 14299. 12830. 12491.
我正在尝试为我拥有的 lat/lon 个值的每个组合计算一些距离。
第一个数据框如下所示:
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
第二个数据框如下所示:
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
所以我想采用 df2
中的每个组合并计算 df1
中每一行的距离并将结果存储为矩阵。我可以使用以下方法计算单个 lat/lon 值的距离:
df1 %>%
add_column(
M_lat = -99.183203,
M_long = 19.506582
) %>%
mutate(
Distance = geosphere::distHaversine(cbind(lon, lat), cbind(M_lat, M_long))
)
不过,我想使用 map
并将结果存储为矩阵,如下所示。
预期输出:
90020001 90030001 90040001 90040010 90040020
Tepozanes 999 111 ... ... ...
Renato Leduc
Samahil
...
Primera ... ... ... ... ...
其中列名来自 df2
中的 GVEGEO
列,行名来自 df1
数据框中的 NOMVIAL
列。这些值是计算的距离。
数据:
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"
))
编辑:
澄清一下,希望更清楚一点。
我想获取 df2
的每一行并用它来计算 df1
中所有行的距离。
取 df2
CVEGEO mean_lat mean_lon
90130143 19.22561 -99.14383
获取这些值并计算 df1
中所有行的距离 - 因此对于 df1
中的 10 行中的每一行,我将根据 [=] 中的这一行计算距离17=]。然后移动到 df2
的第 2 行并再次执行相同的操作...
所以这个矩阵的维度是 5 列和 10 行。
您可以使用以下解决方案。您必须交换第二个数据框中的 mean_lon
和 mean_lat
列,因为在交换 lon
和 lat
时会出现错误。
library(dplyr)
library(purrr)
library(tidyr)
library(tibble)
map2(df4$lon, df4$lat, ~ df5 %>%
rowwise() %>%
mutate(output = geosphere::distHaversine(c(.x, .y), c_across(mean_lon:mean_lat)))) %>%
set_names(df4$NOMVIAL) %>%
map(~ .x %>%
select(CVEGEO, output) %>%
pivot_wider(names_from = CVEGEO, values_from = output)) %>%
bind_rows() %>%
rownames_to_column() %>%
mutate(rowname = df4$NOMVIAL)
# A tibble: 10 x 6
rowname `90130143` `90130234` `90090300` `90130284` `90130290`
<chr> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Tepozanes 35492. 34483. 40636. 35857. 33786.
2 Bartolomé Díaz de León 13513. 5304. 11476. 11549. 11831.
3 Renato Leduc 7820. 14159. 22444. 9385. 6658.
4 Cuautlalpan 4313. 15044. 22501. 7133. 5193.
5 Samahil 10426. 18857. 27048. 12785. 10071.
6 Ninguno 19083. 27775. 36007. 21625. 18974.
7 Monte de Sueve 10065. 17730. 25985. 12194. 9429.
8 Ninguno 20078. 15874. 21699. 19361. 18158.
9 Rinoceronte 27908. 25739. 31724. 27885. 26088.
10 Primera 14334. 7976. 14299. 12830. 12491.