简体   繁体   English

在 R 中提取相交值

[英]Extracting Intersect Values in R

I am wondering if there was a specific way that would allow me to extract the sa3_code_2016 from the QLD sf object and add each corresponding value to the test dataframe.我想知道是否有一种特定的方法可以让我从 QLD sf object 中提取 sa3_code_2016,并将每个相应的值添加到测试 dataframe。

Solved, needed to assign Points with the same CRS ad QLD and do a st_join()已解决,需要使用相同的 CRS 广告 QLD 分配点并执行st_join()

Example data:示例数据:

library(absmapsdata)
library(leaflet)
library(tidyverse)


QLD <- sa32016 %>%
  filter(state_name_2016 == "Queensland") %>% 
  dplyr::select(sa3_code_2016,geometry)

Points <- data.frame(Longitude = c(148.275355, 148.872268, 150.490145),
                  Latitude = c(-23.683148, -21.228157,-22.705135),
                  names = c("First", "Second", "Last"))

Points <- st_as_sf(Points, coords = c("Longitude", "Latitude")) %>% 
  st_set_crs(st_crs(QLD))
  
leaflet() %>% 
  addTiles() %>%
  addPolygons(data=QLD, 
              color = "#b3acc2",
              fillOpacity = 0.5) %>%
  addMarkers(data=Points)


library(sf)
Test <- st_join(Points, QLD)

Your error stems form filtering sa32016 but instead of using the filtered data you again use the unfiltered version in your selection process.您的错误源于过滤 sa32016 但您没有使用过滤后的数据,而是在选择过程中再次使用未过滤的版本。 this leads to your error.这会导致你的错误。 by assigning the full data to QLD and then going through the pipe, your process works just fine.通过将完整数据分配给 QLD,然后通过 pipe,您的流程工作正常。

library(absmapsdata)
library(leaflet)
library(tidyverse)

QLD <- sa32016 # note this step
QLD <- QLD %>%
  filter(state_name_2016 == "Queensland") %>% 
  select(sa3_code_2016,geometry)

name <- c("First", "Second", "Last")
lat <- c(-23.683148, -21.228157,-22.705135)
long <- c(148.275355, 148.872268, 150.490145 )
test <- cbind(name,lat,long)

leaflet() %>% 
  addTiles() %>%
  addPolygons(data=QLD, 
              color = "#b3acc2",
              fillOpacity = 0.5) %>%
  addMarkers(data=test,
             lng = long, 
             lat = lat)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM