'Loosing data using st_join

I am trying to perform a spatial join of two sf shape files.I am losing all information from the second data set (i.e output_inmap). Whichever dataset is placed second will return all NA values. Anyone know what could be happening?

output_inmap <- st_read("processed/ceidars_data_inmap.shp")
output_inmap <-st_transform(output_inmap, crs=3310)

unzip("census-tract.zip")
census_tracts <- st_read("census-tract/tl_2019_06_tract.shp")

st_transform(census_tracts, crs = 3310)
st_transform(output_inmap, crs = 3310)

TC_1<- st_join(census_tracts, output_inmap) 

I am losing all information from the second data set (i.e output_inmap). Whichever dataset is placed second will return all NA values. Anyone know what could be happening?

rsf


Solution 1:[1]

Your second st_transform (of the census tracts) seems to be leading nowhere; consider this code (slightly adjusted via dplyr style pipe) to ensure both spatial objects are on the same CRS.

You may also consider setting parameter left of the sf::st_join() call (by default true) to false = change behaviour from left (preserving) to inner (filtering) style join. Sometimes this makes for a more concise code.

library(sf)
library(dplyr)

output_inmap <- st_read("processed/ceidars_data_inmap.shp") %>%
   st_transform(crs=3310)

unzip("census-tract.zip")
census_tracts <- st_read("census-tract/tl_2019_06_tract.shp") %>%
   st_transform(crs = 3310)


TC_1<- st_join(census_tracts, output_inmap) 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jindra Lacko