r - 如何计算地铁站的乘客数量?
问题描述
你好这是我关于stackoverflow的第一个问题,我希望我做的一切都是正确的。我想数一数地铁站的乘客。我有四个向量,分别是乘客和车站的经度和纬度。我用半径为 20 米的圆圈表示停靠点,用标记表示乘客。现在我想知道圆圈中有多少标记。我已经尝试将标记与markerClusterOption 相加,但圆圈外的标记被添加到圆圈内的标记中,我只想总结圆圈内的标记。我的目标是把圆圈内的标记数加起来。
library(leaflet)
#lat and long of the Subway stations
SubwayStation_long<-c(174.764164,174.764290)
SubwayStation_lat<-c(-36.877022,-36.877844)
#lat and long of the Passengers
Passagier_long<-c(174.764,
174.764436,
174.764336,
174.764044,
174.764034,
174.763,
174.7641,
174.7645,
174.764290,
174.764068,
174.764352,
174.764467)
Passagier_lat<-c(-36.877,
-36.8770099,
-36.8770199,
-36.8770189,
-36.8770189,
-36.876,
-36.8779,
-36.8778,
-36.877844,
-36.877102,
-36.877814,
-36.877900)
tiles = getAllLeafletTiles()
tiles = tiles[c(1,3,27)]
map <- leaflet()
for (provider in tiles) {
map <- map %>% addProviderTiles(provider, group = provider)
}
map <- addLayersControl(
map,
baseGroups = tiles,c("Station","Passengers"),
options = layersControlOptions(collapsed = FALSE))
map <- addMeasure(map, primaryLengthUnit = "kilometers", primaryAreaUnit = "sqmeters", activeColor = "#3D535D", completedColor = "#006400")
map<-addCircles(map,SubwayStation_long,SubwayStation_lat, group = "Station",radius = 15)
map<-addMarkers(map,Passagier_long,Passagier_lat,group = "Passengers",
clusterOptions = markerClusterOptions(freezeAtZoom = FALSE))
map
解决方案
R 有一个包“geosphere”,用于根据两点的经度和纬度计算距离。这个问题是计算每个车站20米范围内有哪些乘客。
library(geosphere)
#create dataframe of data, first column longitude, second latitude
subway<-data.frame(SubwayStation_long, SubwayStation_lat)
passagier<-data.frame(Passagier_long, Passagier_lat)
#proceed row by row and caluclate the distance between the station and each passenger
#return TRUE if the distance is within 20 meters
#first column of output goes with first row of station data.
within20m<-apply(subway, 1, function(x){distGeo(x, passagier)<20})
#number within 20 meters of each station
colSums(within20m)
apply 语句的示例输出。
[,1] [,2]
[1,] TRUE FALSE
[2,] FALSE FALSE
[3,] TRUE FALSE
[4,] TRUE FALSE
[5,] TRUE FALSE
[6,] FALSE FALSE
[7,] FALSE TRUE
[8,] FALSE TRUE
[9,] FALSE TRUE
[10,] TRUE FALSE
[11,] FALSE TRUE
[12,] FALSE TRUE
这应该为合理数量的车站和乘客提供可接受的性能。如果您有大量车站和大量乘客,那么此解决方案将遇到性能问题,并且需要其他工具和技术。
推荐阅读
- python - 将列表追加到熊猫特定单元格中的列表
- reactjs - 从 componentDidMount 调度一个动作
- java - “组语句”中的 JPA 未知列别名
- video - 用于动画图表的视频编解码器 - 帧之间具有微小增量的干净图像
- angular - Angular10,未捕获的类型错误:无法读取 d3.js 中未定义的属性“文档”
- node.js - enableProof 在 passportjs OAuth 2.0 中做了什么?它是否启用pkce?
- r - 如何从变量中获取第一个和最后一个非Inf、非NaN、非NA、非0值?
- flutter - SVG 图片未显示在 Flutter 的 WidgetSpan 中
- transform - timeUnit 在扁平化和泛化转换后不起作用
- python - 我试图绘制水平直方图,但我无法管理刻度。我该如何解决?