R - ggmap - 通过地理编码计算城市之间的最短距离

如何解决R - ggmap - 通过地理编码计算城市之间的最短距离

我有一个城市和相关信息的列表,我已经放在一个数据框中,如下所示:

@Configuration
@Profile("my_profile")
@Import(LibraryConfig::class)
open class MyProfileConfig

使用 my_profilelibrary(plyr) library(dplyr) library(ggmap) library(Imap) cities <- c("washington,dc","wilmington,de","amarillo,tx","denver,co","needham,ma","philadelphia,pa","doylestown,"galveston,"tuscaloosa,al","hollywood,fl" ) id <- c(156952,154222,785695,154423,971453,149888,1356987,178946,169944,136421) month <- c(201811,201811,201912,202005,202106,202106 ) category<- c("home","work","home","cell","cell") places <- data.frame(cities,id,category,month) 包,我可以检索每个城市的经度和纬度:

Imap

我想做的是:

  1. 按月份和类别计算每个城市之间的距离
  2. ggmap 的不同列中返回第二个最短距离以及相应的城市和 ID

我写了一个 lat <- geocode(location = places$cities,source = "google")$lat lon <- geocode(location = places$cities,source = "google")$lon places <- cbind(places,lat,lon) 循环来计算距离:

places

产生以下数据:

for

所需的结果如下所示(第一行):

for (i in 1:nrow(places)) {




dist_list[[i]] <- gdist(lon.1 = places$lon[i],lat.1 = places$lat[i],lon.2 = places$lon,lat.2 = places$lat,units="miles")
  
}

通过 dput(dist_list) list(c(0,98.3464717885451,1386.25425677199,1489.87718040776,383.083760289456,123.232894969413,140.284537078237,1209.23510542932,706.670452283757,906.79542720295),c(98.4762434610638,1472.06660056474,1560.93398322985,285.23618862797,24.9195071209828,44.8853561530985,1308.60741637919,805.755084908157,983.102810248198),c(1389.07354011351,356.573530670257,1712.29111612461,1493.39302974566,1497.2125164277,579.329313217289,827.577713357261,1434.82691622332 ),c(1492.80130415651,1761.3773163288,1578.71125031146,1576.80713231756,923.725006795209,1067.04809350934,1717.32991551111),c(383.551997010915,260.382178510916,243.947043197789,1588.85470703957,1088.38640303169,1230.47219244291),c(123.395655314093,24.9195071209827,24.7382114555287,1333.29925285915,830.581742827321,1002.94777739349 ),c(140.431447025301,44.8853561530986,24.7382114555285,1346.44527983873,844.827513981938,1026.98263808807),c(1211.16392416136,505.292529136012,925.512554201542),c(707.73957320737,666.837848781548),c(906.880841903584,983.102810248198,1434.82691622332,1717.32991551111,1230.47219244291,1002.94777739349,1026.98263808807,925.512554201542,666.837848781548,0)) 中的 cities id category month lat lon min.dist closest city closest city id washington,dc 156952 home 201811 38.90719 -77.03687 98.34647 wilmington,de 154222 函数,我可以获得第二小的距离

nth

我遇到的问题是我不知道如何将列表中的信息连接到 df Rfast。任何帮助或建议将不胜感激。

解决方法

~/.bashrc

分组:

PATH=$HOME/.local/bin:$PATH

结果:

# get min distance:
min_d <- sapply(dist_list,function(x) sort(x)[2])
places$min_dist <- min_d
# index:
i <- sapply(dist_list,function(x) which(sort(x)[2] == x))
# add name:
places$min_name <- places$cities[i]
,

更新

假设我们只按 month 分组,我们可以试试下面的代码

f <- function(df) {
    r <- list()
    for (i in 1:nrow(df)) {
        x <- c()
        for (j in 1:nrow(df)) {
            x <- append(
                x,with(df,gdist(lat[i],lon[i],lat[j],lon[j],units = "miles"))
            )
        }
        x <- replace(x,x == 0,Inf)
        idx <- which.min(x)
        r[[i]] <- data.frame(
            min.dist = min(x),closest_city = df$cities[idx],closest_city_id = df$id[idx]
        )
    }
    do.call(rbind,r)
}

places %>%
    group_by(month) %>%
    do(cbind(.,f(.))) %>%
    ungroup()

给出

# A tibble: 10 x 9
   cities               id category  month   lat    lon min.dist closest_city   
   <chr>             <int> <chr>     <int> <dbl>  <dbl>    <dbl> <chr>
 1 washington,dc   156952 home     201811  38.9  -77.0   104.   wilmington,de
 2 wilmington,de   154222 work     201811  39.7  -75.5   104.   washington,dc
 3 amarillo,tx     785695 home     201912  35.2 -102.    232.   denver,co
 4 denver,co       154423 home     201912  39.8 -105.    232.   amarillo,tx
 5 needham,ma      971453 home     202005  42.3  -71.2   273.   doylestown,pa
 6 philadelphia,~  149888 work     202005  40.0  -75.2     6.81 doylestown,pa
 7 doylestown,pa  1356987 cell     202005  40.3  -75.1     6.81 philadelphia,~
 8 galveston,tx    178946 home     202106  29.2  -94.9 11405.   hollywood,fl
 9 tuscaloosa,al   169944 work     202106  33.2  -87.6   517.   hollywood,fl
10 hollywood,fl    136421 cell     202106  26.0  -80.1   517.   tuscaloosa,al
# ... with 1 more variable: closest_city_id <int>

根据您获得的dist_list,我们可以试试下面的代码

closest <- do.call(
    rbind,lapply(
        dist_list,function(x) {
            x <- replace(x,Inf)
            idx <- which.min(x)
            with(
                places,data.frame(
                    min.dist = min(x),closest_city = cities[idx],closest_city_id = id[idx]
                )
            )
        }
    )
)

给出

    min.dist     closest_city closest_city_id
1   98.34647   wilmington,de          154222
2   24.91951 philadelphia,pa          149888
3  356.57353       denver,co          154423
4  356.57353     amarillo,tx          785695
5  243.94704   doylestown,pa         1356987
6   24.73821   doylestown,pa         1356987
7   24.73821 philadelphia,pa          149888
8  505.29253   tuscaloosa,al          169944
9  505.29253    galveston,tx          178946
10 666.83785   tuscaloosa,al          169944

此外,如果您想将上述数据框附加到您现有的 places,您可以使用

places <- cbind(places,closest)
,

使用sf::st_distance()

鉴于您正在处理空间数据,我建议采用基于空间库的方法,例如 {sf}

library(tidyverse)
library(tidygeocoder)
library(sf)

# clean location,geocode,and convert to sf object
places <- places %>% 
  separate(cities,into = c("city","state"),sep = ",") %>% 
  geocode(city = city,state = state) %>% 
  st_as_sf(coords = c("long","lat"),crs = 4269)

# sanity check
mapview::mapview(places)

enter image description here

# calculate distances between point pairs with st_distance()
compute_close_city <- function(i){
  # compute distances btwn a point and its neighbors (excluding itself)
  distances = st_distance(places[i,],places[-i,])
  # index of the nearest neighbor
  j = which.min(distances)
  
  # organize and return the result
  result <- tibble(
    close_city   = places$city[-i][j],# closest city
    close_state  = places$state[-i][j],# closest state
    close_dist_m = distances[j]         # distance in meters
  )
  
  return(result)
}

# calculate close cities and distances,bind results into dataframe
close_df <- map_df(1:nrow(places),~compute_close_city(.x))

# bind the result to the places data frame
places <- bind_cols(places,close_df)

# view the result and verify it works
select(places,city,close_city,close_dist_m)

返回:

Simple feature collection with 10 features and 3 fields
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: -104.9849 ymin: 26.0112 xmax: -75.13046 ymax: 40.31004
Geodetic CRS:  NAD83
# A tibble: 10 x 4
   city         close_city   close_dist_m             geometry
   <chr>        <chr>                 [m]          <POINT [°]>
 1 washington   wilmington      159476.85 (-77.03656 38.89499)
 2 wilmington   philadelphia     40022.81 (-75.54659 39.74595)
 3 amarillo     denver          574956.04 (-101.8338 35.20722)
 4 denver       amarillo        574956.04 (-104.9849 39.73924)
 5 needham      tuscaloosa      153463.74 (-88.33309 31.98683)
 6 philadelphia doylestown       39775.87 (-75.16353 39.95272)
 7 doylestown   philadelphia     39775.87 (-75.13046 40.31004)
 8 galveston    needham         687140.47 (-94.79459 29.29933)
 9 tuscaloosa   needham         153463.74 (-87.56753 33.20956)
10 hollywood    needham        1035934.60  (-80.14949 26.0112)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res