计算居住在距医院一定距离之内或之外的人数 [英] Calculating the number of people who live within or outside a certain distance from hospitals
本文介绍了计算居住在距医院一定距离之内或之外的人数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是地理空间统计的新手,无法解决一个简单的问题:
I'm new to geospatial stats and can't figure out a simple question:
我有两个具有空间坐标的数据集.一个人在特定地区拥有医院和诊所的坐标.另一个具有该地区所有家庭的坐标.
I have two datasets with spatial coordinates. One has coordinates of hospitals and clinics in a particular district. The other has coordinates of all households in that district.
这里有一些模拟数据
hospital_coord <-data.frame(longitude = c(80.15998, 72.89125, 77.65032, 77.60599),
latitude = c(12.90524, 19.08120, 12.97238, 12.90927))
people_coord <-data.frame(longitude = c(72.89537, 77.65094, 73.95325, 72.96746,
77.65058, 77.66715, 77.64214, 77.58415,
77.76180, 76.65470, 76.65480, 76.65490, 76.65500, 76.65560, 76.65560),
latitude = c(19.07726, 13.03902, 18.50330, 19.16764,
12.90871, 13.01693, 13.00954, 12.92079,
13.02212, 12.81447, 12.81457, 12.81467, 12.81477, 12.81487, 12.81497))
我想计算以下内容:
- 距离最近的诊所/医院超过2公里的家庭有多少百分比
- 在数据框中创建一列,指示哪些家庭在2公里范围之内或之外
推荐答案
我认为这使用最新的sf
软件包而不是链接的问题中的geosphere
可以满足您的要求.方法如下:
I think this does what you want, using the more recent sf
package rather than geosphere
from the question linked. The approach is as follows:
- 使用
st_as_sf
将纬度/经度点转换为几何对象
- 将坐标参考系统设置为标准的长/纬度,因为数据为长/纬度(这是WGS84)
- 使用
st_distance
来计算每个人与每家医院之间的距离,作为units
表,以米为单位. - 将这张
units
表格转换为常规tbl
,因为这很麻烦,并检查哪些对之间的距离超过2公里 - 使用
mutate_at
来检查每一行,以查看每个医院是否在2公里之内(T
)或2公里以上(F
) - 最后,使用
pmap
和any
检查每一行,看看是否至少 一家医院在2公里之内!
- Convert the latitude/longitude points into geometry objects using
st_as_sf
- Set the coordinate reference system to a standard long/lat one since the data is in long/lat (this is WGS84)
- Use
st_distance
to compute the distance between each person and each hospital as aunits
table, in metres. - Convert that
units
table into a regulartbl
because it is a pain to deal with, and check which pairs have more than 2km separation - Use
mutate_at
to check each row to see whether each hospital is less than 2km away (T
) or more than 2km away (F
) - Finally, use
pmap
andany
to check each row and see if at least one hospital is within 2km!
看起来只有第一位病人在医院的2公里之内.
It looks like only the first patient is within 2km of a hospital.
library(tidyverse)
library(sf)
hospital <- tibble(
longitude = c(80.15998, 72.89125, 77.65032, 77.60599),
latitude = c(12.90524, 19.08120, 12.97238, 12.90927)
)
people <- tibble(
longitude = c(72.89537, 77.65094, 73.95325, 72.96746, 77.65058,
77.66715, 77.64214, 77.58415, 77.76180, 76.65470,
76.65480, 76.65490, 76.65500, 76.65560, 76.65560),
latitude = c(19.07726, 13.03902, 18.50330, 19.16764, 12.90871,
13.01693, 13.00954, 12.92079, 13.02212, 12.81447,
12.81457, 12.81467, 12.81477, 12.81487, 12.81497)
)
hospital_sf <- hospital %>%
st_as_sf(coords = c("longitude", "latitude")) %>%
st_set_crs(4326)
people_sf <- people %>%
st_as_sf(coords = c("longitude", "latitude")) %>%
st_set_crs(4326)
distances <- st_distance(people_sf, hospital_sf) %>%
as_tibble() %>%
mutate_at(vars(V1:V4), as.numeric) %>%
mutate_at(vars(V1:V4), function (x) x > 2000) %>%
mutate(within_2km = pmap_lgl(., function(V1, V2, V3, V4) any(V1, V2, V3, V4)))
# A tibble: 15 x 5
V1 V2 V3 V4 within_2km
<lgl> <lgl> <lgl> <lgl> <lgl>
1 T F T T T
2 T T T T F
3 T T T T F
4 T T T T F
5 T T T T F
6 T T T T F
7 T T T T F
8 T T T T F
9 T T T T F
10 T T T T F
11 T T T T F
12 T T T T F
13 T T T T F
14 T T T T F
15 T T T T F
这篇关于计算居住在距医院一定距离之内或之外的人数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文