【问题标题】:Counting points when buffers are overlapping缓冲区重叠时的计数点
【发布时间】:2021-12-14 22:25:02
【问题描述】:

我在下面包含了我的所有代码和示例数据的链接。

简要说明:我有重叠的缓冲区;我想统计距离学校一定米数范围内的店铺数量。

我特别想知道离学校1000米以内有多少家店,离学校2000米以内有多少家店,我想比较一下区别。当然,其中一些学校缓冲区重叠。所以一个店铺可能距离A学校1500m,而距离B学校只有750m,所以算作距离学校1000m以内,只算B校1000m以内,而不是计入学校 A。如果商店在两所学校的 2000 m 范围内(但不在 1000 m 范围内),则需要计入离它最近的学校。

所以理想情况下,我希望数据集看起来像:

School Stores1000m Stores2000m
School A 3 6
School B 2 7

所以我在 sf 中使用了 st_union 函数来合并缓冲区。这对于生成漂亮的地图很有效,但是当我使用长度和 st_intersects 来计算缓冲区内的商店时,它只为每种类型的区域返回一个数字(1000 m vs 2000 m)

样本数据:Sample data

county.sf <- get_acs(state = "MO",
                     county = c( "St. Louis City"),
                     geography = "tract",
                     variables = "B03002_001", 
                     output="wide", 
                     geometry = TRUE) %>%
  sf::st_transform(crs = "ESRI:102003")
  
class(county.sf)

# School data
school <- read.csv("C:\\myfile1.csv")
school.sf <- st_as_sf(school, coords = c("long", "lat"), crs = "epsg:4326") 
school.sf.utm <- st_transform(school.sf, crs = "ESRI:102003")


# Store data
store <- import("C:\\myfile2.csv")
store.sf <- st_as_sf(store, coords = c("XCoord", "YCoord"), crs = "ESRI:102696") 
store.sf.utm <- st_transform(store.sf, crs = "ESRI:102003")


elem.buff <-st_buffer(school.sf.utm, 1000)     
elem.buff2 <-st_buffer(school.sf.utm, 2000) 

pts_com<-st_union(elem.buff)
pts_pol<-st_cast(pts_com, "POLYGON")

pts_com2<-st_union(elem.buff2)
pts_pol2<-st_cast(pts_com2, "POLYGON")


#unmerged zone map
ex.map<- tm_shape(county.sf) +
  tm_polygons() + 
  
  tm_shape(elem.buff) +
  tm_borders(col="red") +  
  
  tm_shape(school.sf.utm) +
  tm_dots(col = "red") +
  
  tm_shape(elem.buff2) +
  tm_borders(col="blue") + 
    
  tm_shape(pts_pol) +
  tm_borders(col="black") +
  
  tm_shape(store.sf.utm) +
  tm_dots() 
ex.map




#merged zones map

ex.map<- tm_shape(county.sf) +
  tm_polygons() + 
  
  #(elem.buff) +
  #tm_borders(col="red") +  
  
  tm_shape(school.sf.utm) +
  tm_dots(col = "red") +
  
  #tm_shape(elem.buff2) +
  #tm_borders(col="blue") + 
  
  tm_shape(pts_pol) +
  tm_borders(col="red") +
  
  tm_shape(store.sf.utm) +
  tm_dots() +

  tm_shape(pts_pol2) +
  tm_borders(col="blue")
ex.map



(school$pt_count <- lengths(st_intersects(elem.buff, store.sf.utm))) #gives per school but ignores overlapping
(school$pt_count <- lengths(st_intersects(pts_com, store.sf.utm)))

(school$pt_count <- lengths(st_intersects(elem.buff2, store.sf.utm)))
(school$pt_count <- lengths(st_intersects(pts_com2, store.sf.utm)))

【问题讨论】:

  • 您好 revere2323。 我在下面包含了我的所有代码和示例数据的链接。 虽然这对您来说似乎是个好主意,但这让回答这个问题变得非常困难。 reproducible exampleminimal reproducible example 在您的问题中包含示例输入将增加您获得问题答案的机会。
  • 我不知道我怎么能在这里做到这一点。这是相当具体的,因为需要有重叠的缓冲区,并且存储也需要落入这些缓冲区中才能使示例有意义。我只包括了一个学校的邮政编码,所以我可以做到的尽可能少。确实,我只需要假设性地知道如何做到这一点——但谢谢你的建议,如果我能找到一种方法来简化它,我会继续努力的。
  • 如果您一心想要避免重复计算 - 即每家商店都需要正好有一所最近的学校 - 缓冲可能不是您的最佳方法。考虑 st_nearest_feature(获取最近的学校),然后是 st_distance(测量其距离)。

标签: r geospatial sf geo


【解决方案1】:

针对更大数据的更新、更有效的答案:

我承认,之前的答案依赖于制作所有学校的list 列,并使用unnest() 查找每个组合不适合更大的数据。

正如@JindraLacko 在 cmets 中所建议的,st_nearest_feature() 是您的朋友;不出所料,它比我提出的“手动”方法更有效。

如上,加载库和数据

library(readxl)
library(tidyverse)
library(sf)

library(tmap)
tmap_mode('view')


read_xlsx('Schools and Stores_all.xlsx', sheet = 1) %>% 
  st_as_sf(., coords = c("long", "lat"), crs = "epsg:4326") %>% 
  st_transform(crs = "ESRI:102003") %>% 
  {. ->> school.sf.utm}

read_xlsx('Schools and Stores_all.xlsx', sheet = 2) %>% 
  st_as_sf(., coords = c("XCoord", "YCoord"), crs = "ESRI:102696") %>% 
  st_transform(crs = "ESRI:102003") %>% 
  {. ->> store.sf.utm}

然后,我们使用st_join() 加入商店和学校数据,并指定join = st_nearest_feature 以便加入(名称)每个商店最近的学校。然后我们加入每个学校的几何使用left_join()。有关详细信息,请参阅?st_join。所以最终,这为我们提供了每家商店最近的学校。

# find the closest school to each store (this is the school it counts towards)
store.sf.utm %>% 
  rename(
    store_geometry = geometry
  ) %>% 
  st_join(
    school.sf.utm, 
    join = st_nearest_feature
  ) %>% 
  left_join(
    school.sf.utm %>% 
      as_tibble %>% 
      rename(
        school_geometry = geometry
      )
  ) %>% 
  {. ->> all_combos}

all_combos

# Simple feature collection with 2603 features and 1 field
# Active geometry column: store_geometry
# Geometry type: POINT
# Dimension:     XY
# Bounding box:  xmin: 489948.3 ymin: 131719.1 xmax: 501438.8 ymax: 157382.1
# Projected CRS: USA_Contiguous_Albers_Equal_Area_Conic
# # A tibble: 2,603 x 3
#        store_geometry School                               school_geometry
#           <POINT [m]> <chr>                                    <POINT [m]>
# 1  (489948.3 137420.8) Community Access Job Training    (491117.8 136616.5)
# 2  (490119.7 136712.7) Community Access Job Training    (491117.8 136616.5)
# 3  (490171.8 138758.2) Gateway Science Acad/st Louis    (491307.4 138787.2)
# 4  (490370.2 139681.3) Wilkinson Early Childhood Center (490930.4 140461.2)
# 5  (490568.3 137056.8) Community Access Job Training    (491117.8 136616.5)
# 6    (490475 139013.4) Gateway Science Acad/st Louis    (491307.4 138787.2)
# 7  (490527.6 139633.1) Wilkinson Early Childhood Center (490930.4 140461.2)
# 8  (490715.3 136690.1) Community Access Job Training    (491117.8 136616.5)
# 9  (490552.5 139805.9) Wilkinson Early Childhood Center (490930.4 140461.2)
# 10   (490790 138069.5) Gateway Science Acad/st Louis    (491307.4 138787.2)
# # ... with 2,593 more rows

有趣的是,我们已经从 156 所学校减少到 134 所。我假设这意味着有 22 所学校不是离任何商店最近的学校。

# how many schools in all_combos?
all_combos %>% 
  summarise(
    n_schools = n_distinct(School)
  ) %>% 
  pull(n_schools)

# [1] 134

现在我们知道哪所学校最近,计算每家商店与它最近的学校之间的距离。

# calculate distance from each store to each school
all_combos %>% 
  mutate(
    distance = as.numeric(st_distance(store_geometry, school_geometry, by_element = TRUE))
  ) %>% 
  filter(
    distance <= 2000
  ) %>% 
  {. ->> all_combos_2}

all_combos_2

# Simple feature collection with 2595 features and 2 fields
# Active geometry column: store_geometry
# Geometry type: POINT
# Dimension:     XY
# Bounding box:  xmin: 489948.3 ymin: 131719.1 xmax: 501438.8 ymax: 152889.7
# Projected CRS: USA_Contiguous_Albers_Equal_Area_Conic
# # A tibble: 2,595 x 4
#        store_geometry School                               school_geometry distance
# *          <POINT [m]> <chr>                                    <POINT [m]>    <dbl>
# 1  (489948.3 137420.8) Community Access Job Training    (491117.8 136616.5)    1419.
# 2  (490119.7 136712.7) Community Access Job Training    (491117.8 136616.5)    1003.
# 3  (490171.8 138758.2) Gateway Science Acad/st Louis    (491307.4 138787.2)    1136.
# 4  (490370.2 139681.3) Wilkinson Early Childhood Center (490930.4 140461.2)     960.
# 5  (490568.3 137056.8) Community Access Job Training    (491117.8 136616.5)     704.
# 6    (490475 139013.4) Gateway Science Acad/st Louis    (491307.4 138787.2)     863.
# 7  (490527.6 139633.1) Wilkinson Early Childhood Center (490930.4 140461.2)     921.
# 8  (490715.3 136690.1) Community Access Job Training    (491117.8 136616.5)     409.
# 9  (490552.5 139805.9) Wilkinson Early Childhood Center (490930.4 140461.2)     756.
# 10   (490790 138069.5) Gateway Science Acad/st Louis    (491307.4 138787.2)     885.
# # ... with 2,585 more rows

算出距离最近的学校 1000 m 范围内有多少家商店。

all_combos_2 %>%
  filter(
    distance <= 1000
  ) %>% 
  group_by(School) %>% 
  summarise(
    Stores1000m = n()
  ) %>% 
  st_drop_geometry %>% 
  {. ->> combo_sum_1000}

combo_sum_1000

# # A tibble: 134 x 2
#   School                                    Stores1000m
# * <chr>                                           <int>
# 1 Academy At Boys & Girls Town                       24
# 2 AcademyOf Envt Sci/math Elementary School          18
# 3 AcademyOf Envt Sci/math Middle School               2
# 4 Adams Elementary School                            12
# 5 Ames Visual/perf. Arts                             25
# 6 Ashland Elementary And Br.                         49
# 7 Aspire Academy                                     26
# 8 Beaumont Cte High School                           46
# 9 Bishop DuBourg High School                          4
# 10 Bryan Hill Elementary School                       19
# # ... with 124 more rows

2000 米也是如此。

all_combos_2 %>%
  filter(
    distance <= 2000
  ) %>% 
  group_by(School) %>% 
  summarise(
    Stores2000m = n()
  ) %>% 
  st_drop_geometry %>% 
  {. ->> combo_sum_2000}

combo_sum_2000

# # A tibble: 134 x 2
#   School                                    Stores2000m
# * <chr>                                           <int>
# 1 Academy At Boys & Girls Town                       24
# 2 AcademyOf Envt Sci/math Elementary School          18
# 3 AcademyOf Envt Sci/math Middle School               2
# 4 Adams Elementary School                            12
# 5 Ames Visual/perf. Arts                             25
# 6 Ashland Elementary And Br.                         49
# 7 Aspire Academy                                     28
# 8 Beaumont Cte High School                           52
# 9 Bishop DuBourg High School                          4
# 10 Bryan Hill Elementary School                       19
# # ... with 124 more rows

然后将两个表连接在一起。

combo_sum_1000 %>% 
  full_join(combo_sum_2000) %>% 
  {. ->> combo_sum_joined}

combo_sum_joined

# # A tibble: 134 x 3
#    School                                    Stores1000m Stores2000m
#    <chr>                                           <int>       <int>
# 1  Academy At Boys & Girls Town                       24          24
# 2  AcademyOf Envt Sci/math Elementary School          18          18
# 3  AcademyOf Envt Sci/math Middle School               2           2
# 4  Adams Elementary School                            12          12
# 5  Ames Visual/perf. Arts                             25          25
# 6  Ashland Elementary And Br.                         49          49
# 7  Aspire Academy                                     26          28
# 8  Beaumont Cte High School                           46          52
# 9  Bishop DuBourg High School                          4           4
# 10 Bryan Hill Elementary School                       19          19
# # ... with 124 more rows

【讨论】:

    【解决方案2】:

    如果我正确解释了问题,我想我有答案了。

    根据我从问题中得到的信息,您想知道在每所学校的 1000 和 2000 米范围内有多少家商店,但商店只计入离它们最近的学校 - 这是对吧?

    最少的代码设置,将您的示例数据保存为工作目录中的.xlsx 文件:

    library(readxl)
    library(tidyverse)
    library(sf)
    
    read_xlsx('Schools and Stores.xlsx', sheet = 1) %>% 
      st_as_sf(., coords = c("long", "lat"), crs = "epsg:4326") %>% 
      st_transform(crs = "ESRI:102003") %>% 
      {. ->> school.sf.utm}
    
    read_xlsx('Schools and Stores.xlsx', sheet = 2) %>% 
      st_as_sf(., coords = c("XCoord", "YCoord"), crs = "ESRI:102696") %>% 
      st_transform(crs = "ESRI:102003") %>% 
      {. ->> store.sf.utm}
    

    首先,为了减少数据集中的商店数量,我们只在所有学校的 2 公里缓冲区内保留商店(这可能是您在 st_buffer() 之后使用 st_union() 所做的)。这将商店数量从 2603 家减少到 191 家。

    # step 1 - keep only stores within a 2km buffer of all schools, to reduce number of stores to work with
    stores.sf.utm %>% 
      filter(
        st_intersects(stores.sf.utm, school.sf.utm %>% st_buffer(2000), sparse = FALSE)
      ) %>% 
      rename(
        geometry_stores = geometry
      ) %>% 
      {. ->> stores_2000}
    
    stores_2000
    
    # Simple feature collection with 191 features and 0 fields
    # Geometry type: POINT
    # Dimension:     XY
    # Bounding box:  xmin: 496820.2 ymin: 138115.8 xmax: 500484.2 ymax: 141987.8
    # Projected CRS: USA_Contiguous_Albers_Equal_Area_Conic
    # # A tibble: 191 x 1
    #       geometry_stores
    #           <POINT [m]>
    # 1   (496820.2 139441)
    # 2 (496848.1 140725.7)
    # 3 (496987.8 138959.5)
    # 4 (497052.2 139815.4)
    # 5   (497030 140286.7)
    # 6 (497122.5 138900.1)
    # 7 (497033.2 140646.1)
    # 8 (497099.8 140279.6)
    # 9 (497199.7 138687.5)
    # 10 (497154.4 139805.9)
    # # ... with 181 more rows
    

    接下来,我们生成学校和剩余商店的所有潜在组合。我分配了一个store_id,这样我们就可以知道哪个商店是哪个(不使用它是geometry)。

    # generate all schools~stores combos
    stores_2000 %>% 
      mutate(
        store_id = row_number(),
        schools = list(school.sf.utm)
      ) %>% 
      unnest(cols = c('schools')) %>% 
      rename(
        geometry_school = geometry
      ) %>% 
      {. ->> all_combos}
    
    all_combos
    
    # Simple feature collection with 3438 features and 2 fields
    # Active geometry column: geometry_stores
    # Geometry type: POINT
    # Dimension:     XY
    # Bounding box:  xmin: 496820.2 ymin: 138115.8 xmax: 500484.2 ymax: 141987.8
    # Projected CRS: USA_Contiguous_Albers_Equal_Area_Conic
    # # A tibble: 3,438 x 4
    #      geometry_stores store_id School                                    geometry_school
    #          <POINT [m]>    <int> <chr>                                         <POINT [m]>
    #  1 (496820.2 139441)        1 AcademyOf Envt Sci/math Middle School (498610.1 140067.7)
    #  2 (496820.2 139441)        1 Collegiate School Of Med/bio          (496797.7 140597.6)
    #  3 (496820.2 139441)        1 Dewey Sch.-internat'l. Studies        (499626.5 139130.3)
    #  4 (496820.2 139441)        1 Eagle Fox Park                        (498015.9 139324.1)
    #  5 (496820.2 139441)        1 Education Therap Support At Madison   (476270.1 131682.7)
    #  6 (496820.2 139441)        1 Hodgen Elementary School              (497853.4 140290.1)
    #  7 (496820.2 139441)        1 Humboldt Academy Of Higher Lrning     (499410.4 138707.3)
    #  8 (496820.2 139441)        1 Lafayette Preparatory Academy           (498812.6 140006)
    #  9 (496820.2 139441)        1 Lift For Life Academy                 (500025.8 139526.4)
    # 10 (496820.2 139441)        1 Lift For Life Academy High School     (500025.8 139526.4)
    # # ... with 3,428 more rows
    

    这意味着我们可以计算出每家商店到每所学校的距离。然后,我们只保留彼此相距 2000 m 以内的组合(这些组合由原始 2 km 缓冲区对面的商店和学校组成,这就是它们的距离超过 2 km 的原因)。

    # calculate distance from each store to each school
    all_combos %>% 
      mutate(
        distance = as.numeric(st_distance(geometry_stores, geometry_school, by_element = TRUE))
      ) %>% 
      filter(
        distance <= 2000
      ) %>% 
      {. ->> all_combos_2}
    
    all_combos_2
    
    # Simple feature collection with 2231 features and 3 fields
    # Active geometry column: geometry_stores
    # Geometry type: POINT
    # Dimension:     XY
    # Bounding box:  xmin: 496820.2 ymin: 138115.8 xmax: 500484.2 ymax: 141987.8
    # Projected CRS: USA_Contiguous_Albers_Equal_Area_Conic
    # # A tibble: 2,231 x 5
    #        geometry_stores store_id School                                    geometry_school distance
    # *          <POINT [m]>    <int> <chr>                                         <POINT [m]>    <dbl>
    # 1    (496820.2 139441)        1 AcademyOf Envt Sci/math Middle School (498610.1 140067.7)   1896. 
    # 2    (496820.2 139441)        1 Collegiate School Of Med/bio          (496797.7 140597.6)   1157. 
    # 3    (496820.2 139441)        1 Eagle Fox Park                        (498015.9 139324.1)   1201. 
    # 4    (496820.2 139441)        1 Hodgen Elementary School              (497853.4 140290.1)   1337. 
    # 5    (496820.2 139441)        1 Mckinley Class. Leadership Ac.        (498355.8 139560.4)   1540. 
    # 6    (496820.2 139441)        1 Nahed Chapman New American Academy    (496615.8 140605.6)   1182. 
    # 7    (496820.2 139441)        1 Shenandoah Elementary School            (496821 139360.4)     80.6
    # 8    (496820.2 139441)        1 Sigel Elementary Comm. Ed. Center     (498603.2 139613.7)   1791. 
    # 9    (496820.2 139441)        1 St. Louis Christian Academy           (497245.5 140196.9)    867. 
    # 10 (496848.1 140725.7)        2 AcademyOf Envt Sci/math Middle School (498610.1 140067.7)   1881. 
    # # ... with 2,221 more rows
    

    现在,如果我的理解是正确的,每家商店只计入离它最近的学校。所以,我们只保留每家店离学校最近的使用filter()

    # first, keep only the closest school to each store
    all_combos_2 %>% 
      arrange(store_id, distance) %>% 
      group_by(store_id) %>% 
      filter(
        distance == min(distance)
      ) %>% 
      {. ->> all_combos_3}
    # so now we have the closest school to each store
    
    all_combos_3
    
    # Simple feature collection with 223 features and 3 fields
    # Active geometry column: geometry_stores
    # Geometry type: POINT
    # Dimension:     XY
    # Bounding box:  xmin: 496820.2 ymin: 138115.8 xmax: 500484.2 ymax: 141987.8
    # Projected CRS: USA_Contiguous_Albers_Equal_Area_Conic
    # # A tibble: 223 x 5
    # # Groups:   store_id [191]
    #        geometry_stores store_id School                           geometry_school distance
    # *          <POINT [m]>    <int> <chr>                                <POINT [m]>    <dbl>
    # 1    (496820.2 139441)        1 Shenandoah Elementary School   (496821 139360.4)     80.6
    # 2  (496848.1 140725.7)        2 Collegiate School Of Med/bio (496797.7 140597.6)    138. 
    # 3  (496987.8 138959.5)        3 Shenandoah Elementary School   (496821 139360.4)    434. 
    # 4  (497052.2 139815.4)        4 St. Louis Christian Academy  (497245.5 140196.9)    428. 
    # 5    (497030 140286.7)        5 St. Louis Christian Academy  (497245.5 140196.9)    233. 
    # 6  (497122.5 138900.1)        6 Shenandoah Elementary School   (496821 139360.4)    550. 
    # 7  (497033.2 140646.1)        7 Collegiate School Of Med/bio (496797.7 140597.6)    240. 
    # 8  (497099.8 140279.6)        8 St. Louis Christian Academy  (497245.5 140196.9)    168. 
    # 9  (497199.7 138687.5)        9 Shenandoah Elementary School   (496821 139360.4)    772. 
    # 10 (497154.4 139805.9)       10 St. Louis Christian Academy  (497245.5 140196.9)    402. 
    # # ... with 213 more rows
    

    请注意,我们现在有 223 行。这意味着有 32 个重复项 (223 - 191);有两所(或更多)最近的学校,并且它们与商店的距离相同(在本例中,最大重复次数 = 2)。但是,您选择处理这些取决于您。在本例中,我会将它们留在数据中,但如果您只想要一所学校,您可以按字母顺序选择第一个或随机选择等。

    所以现在,我们可以计算出距离(最近的)学校 1000 m 范围内有多少家商店:

    # now, how many closest stores are within 1000 m of each school
    all_combos_3 %>% 
      filter(
        distance <= 1000
      ) %>% 
      group_by(School) %>% 
      summarise(
        Stores1000m = n()
      ) %>% 
      st_drop_geometry %>% 
      {. ->> combo_sum_1000}
    
    combo_sum_1000
    
    # # A tibble: 16 x 2
    #    School                                Stores1000m
    #  * <chr>                                       <int>
    #  1 AcademyOf Envt Sci/math Middle School           2
    #  2 Collegiate School Of Med/bio                    4
    #  3 Dewey Sch.-internat'l. Studies                  6
    #  4 Eagle Fox Park                                 37
    #  5 Hodgen Elementary School                       17
    #  6 Humboldt Academy Of Higher Lrning              10
    #  7 Lafayette Preparatory Academy                   1
    #  8 Lift For Life Academy                           8
    #  9 Lift For Life Academy High School               8
    # 10 Mckinley Class. Leadership Ac.                  7
    # 11 Peabody Elementary School                      48
    # 12 Shenandoah Elementary School                    6
    # 13 Sigel Elementary Comm. Ed. Center               7
    # 14 St. Louis Christian Academy                     7
    # 15 St. Louis College Prep High School             14
    # 16 St. Louis College Prep Middle School           14
    

    对于 2000 米以内的店铺,同样的做法:

    # 2000 m
    all_combos_3 %>% 
      filter(
        distance <= 2000
      ) %>% 
      group_by(School) %>% 
      summarise(
        Stores2000m = n()
      ) %>% 
      st_drop_geometry %>% 
      {. ->> combo_sum_2000}
    
    combo_sum_2000
    
    # # A tibble: 16 x 2
    #    School                                Stores2000m
    #  * <chr>                                       <int>
    #  1 AcademyOf Envt Sci/math Middle School           2
    #  2 Collegiate School Of Med/bio                    4
    #  3 Dewey Sch.-internat'l. Studies                  6
    #  4 Eagle Fox Park                                 37
    #  5 Hodgen Elementary School                       18
    #  6 Humboldt Academy Of Higher Lrning              10
    #  7 Lafayette Preparatory Academy                   1
    #  8 Lift For Life Academy                           8
    #  9 Lift For Life Academy High School               8
    # 10 Mckinley Class. Leadership Ac.                  7
    # 11 Peabody Elementary School                      53
    # 12 Shenandoah Elementary School                    7
    # 13 Sigel Elementary Comm. Ed. Center               7
    # 14 St. Louis Christian Academy                     7
    # 15 St. Louis College Prep High School             24
    # 16 St. Louis College Prep Middle School           24
    

    当然,我们可以加入这两个数据集来匹配您想要的输出。

    combo_sum_1000 %>% 
      full_join(combo_sum_2000) %>% 
      {. ->> combo_sum_joined}
    
    combo_sum_joined
    
    # # A tibble: 16 x 3
    #    School                                Stores1000m Stores2000m
    #    <chr>                                       <int>       <int>
    #  1 AcademyOf Envt Sci/math Middle School           2           2
    #  2 Collegiate School Of Med/bio                    4           4
    #  3 Dewey Sch.-internat'l. Studies                  6           6
    #  4 Eagle Fox Park                                 37          37
    #  5 Hodgen Elementary School                       17          18
    #  6 Humboldt Academy Of Higher Lrning              10          10
    #  7 Lafayette Preparatory Academy                   1           1
    #  8 Lift For Life Academy                           8           8
    #  9 Lift For Life Academy High School               8           8
    # 10 Mckinley Class. Leadership Ac.                  7           7
    # 11 Peabody Elementary School                      48          53
    # 12 Shenandoah Elementary School                    6           7
    # 13 Sigel Elementary Comm. Ed. Center               7           7
    # 14 St. Louis Christian Academy                     7           7
    # 15 St. Louis College Prep High School             14          24
    # 16 St. Louis College Prep Middle School           14          24
    

    我希望我对问题的解释是正确的,我承认这有点令人困惑,因为我们在按商店和学校等分组之间切换。但我认为这是可行的。

    【讨论】:

    • 非常感谢!明天将对此进行测试并回复您,但这太棒了,我想我正在寻找什么。
    • 好吧@hugh-allan,你是救命稻草,但无论出于何种原因,当我在整个数据集上使用它时,我总共只得到了 20 所学校。请注意几件事:(1)我提供了一个邮政编码相同的学校样本,但我在整个圣路易斯市都有学校。我总共有 157 所学校。所以我不确定这是否会影响这一点,但我不明白为什么会这样。只有20所学校的最终结果肯定不对我整天都在努力解决问题,我想不出我们的问题是什么。让我知道是否有帮助,我可以将所有学校添加到示例数据中。
    • 如果你提供完整的数据,我有机会可以看看。第一个问题 - 您是否 100% 确定商店 2 公里范围内有 20 多所学校?这听起来很明显,但例如映射它们可能是检查投影是否正确等的好方法。假设它们是正确的,看看你是否能找到学校丢失的阶段(stores_2000all_combos,@ 987654337@ 等)。如果您愿意,可以将完整数据添加到问题中。
    • 我在上面的链接中添加了完整的数据。有156所学校。 all_combos 显示 3120 ob​​s,all_combos_2 显示 46,all_combos_3 显示 20...所以不确定。而且我 100% 确定在 2000m 范围内还有更多学校,因为我已经绘制了城市和商店的地图。
    • 嗨@hugh-allan。这行得通!它给了我我需要的东西。这是一个奇怪的要求,但我肯定会在我的论文演讲和这项工作产生的任何相应论文中包括你作为致谢。为了互联网,我已经更改了变量名,所以最终结果的解释会与这里有所不同,但你的帮助非常大,我不知道如何表达感激之情。如果您不想被承认,请告诉我。我会提前告诉你结论:)
    猜你喜欢
    • 2014-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-04
    • 1970-01-01
    • 2016-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多