You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在R中处理GTFS的stoptimes表,生成站点关联线路列表?

R中实现站点与途经线路关联及后续分析的方法

1. 构建站点-途经线路关联数据框

针对你提供的stoptimes数据,可通过分组汇总的方式,将每个站点与途经它的线路列表关联起来,以下是两种常用实现方法:

方法1:使用dplyr(tidyverse生态)

先确保已安装并加载dplyr包:

install.packages("dplyr")
library(dplyr)

处理代码:

# 示例数据
stoptimes <- data.frame(route = c("route1", "route1", "route1", "route2", "route2", "route2", "route3", "route3", "route3"),
                        stops = c("stop1", "stop2", "stop3", "stop3", "stop2", "stop1", "stop3", "stop4", "stop5"))

# 按站点分组,汇总去重后的途经线路列表
stop_routes <- stoptimes %>%
  group_by(stops) %>%
  summarize(
    routes = list(unique(route)),  # 用unique避免同一站点重复记录同一条线路
    .groups = "drop"
  )

# 查看结果
print(stop_routes)

输出结果中,routes列为列表类型,每个元素对应该站点途经的所有线路向量。

方法2:使用Base R

无需额外安装包,直接用aggregate函数实现:

stop_routes_base <- aggregate(route ~ stops, data = stoptimes, FUN = function(x) list(unique(x)))

2. 合并站点位置信息

假设你有包含站点经纬度的stop_locations数据框,可通过merge函数合并线路信息与位置信息:

# 示例站点位置数据(替换为你的实际数据)
stop_locations <- data.frame(
  stops = c("stop1", "stop2", "stop3", "stop4", "stop5"),
  lon = c(116.3, 116.35, 116.4, 116.45, 116.5),
  lat = c(39.9, 39.92, 39.95, 39.97, 40.0)
)

# 合并数据
stop_info <- merge(stop_routes, stop_locations, by = "stops")

3. 统计特定点半径范围内的不同线路数量

使用geosphere包计算球面距离,筛选范围内站点后统计线路数:

先安装并加载geosphere:

install.packages("geosphere")
library(geosphere)

处理代码:

# 设置目标点坐标与半径(单位:米)
target_lon <- 116.4  # 目标点经度
target_lat <- 39.95  # 目标点纬度
radius <- 1000       # 1000米半径

# 计算每个站点到目标点的距离
stop_info$distance <- distHaversine(
  stop_info[, c("lon", "lat")],
  c(target_lon, target_lat)
)

# 提取半径内站点的所有线路,去重后统计数量
nearby_unique_routes <- stop_info %>%
  filter(distance <= radius) %>%
  pull(routes) %>%
  unlist() %>%
  unique()

unique_route_count <- length(nearby_unique_routes)
cat("目标点半径范围内的不同线路数量:", unique_route_count, "\n")

内容的提问来源于stack exchange,提问作者Monelisa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 21:53:13