I'm working on converting an old script from using sp() to sf() and some of the computations have to be rewritten, including some operations which (used to) use cbind
with over()
like so:
cbind(df1data, over(df1 df2))
I've tried a number of combinations of st_intersects
, st_join
, and merge_cols
, and can't seem to get the columns added on from the second sf()
.
Here's a reproducible example:
# read in relevant polygons, Scottish Index of Multiple deprivation
if (file.exists("data/sc_dz_11.shp") == FALSE) {
download.file("http://simd.scot/2016/data/simd2016_withgeog.zip",
destfile = "data/simd2016_withgeog.zip")
unzip("data/simd2016_withgeog.zip", exdir = "data", junkpaths = TRUE)
}
simd_shapes <- st_read("data/sc_dz_11.shp")
simd_indicators <- read.csv("./data/simd2016_withinds.csv")
simd_indicators_min <- simd_indicators[c(1,6:17)]
# Original data is in wgs, so need to convert from wgs to bng to work with analysis across data sets
simd_wgs <- merge(x=simd_shapes, y=simd_indicators_min, by.x = "DataZone", by.y = "Data_Zone")
simd <- simd_wgs %>% st_transform(27700)
c1 <- c("site1","site2","site3")
c2 <- c("357109","350477.6", "257916")
c3 <- c("734754","614926.268", "662462")
df <- data.frame(c1, c2, c3, c3)
df_joined <- cbind(df@data, over(df, simd)) # of course this is the part where I'm struggling
df_joined <- bind_cols(df,st_intersects(df,simd)) # tried this too and it also didn't work
Very grateful if anyone can shine some light on what I'm getting wrong here!
sf
can work as a regular data.frame. Have you triedcbind(simd, df )/ bind_cols(simd, df )
? If both datasets have the same number of rows it should worksf
objects (not the case on your example,df
is a regular data frame, isn’t it?) stackoverflow.com/questions/71344590/…