Join a shapefile with a data.frame with missing data in data.frame using R.

I'm trying to join a data frame with a shapefile using R. My shapefile has 7 features, corresponding to 7 regions of my city.
My data.frame has reptile data in 6 regions of my city, but in one region no reptiles were found.

I'm using the sp::merge command to join the shapefile and data.frame, but when I do the union, the region that doesn't have a reptile doesn't appear.

#load data.frame
serpentes<-read_excel('E:/22-serpentes_cg/R/serpentes_cg_bioterio_ucdb_final.xlsx')
#View(serpentes)

#calc data
#Total de Serpente
total_serpentes<-serpentes_cg%>%
group_by(regiao_cg)%>%
summarise(Total=sum(quant))
View(total_serpentes)

#load shapefile
regiao<-sf::st_read("E:/22-serpentes_cg/geo/regioes_urbanas.shp")%>%
rename(regiao_cg=REGIAO_CG)
View(regiao)

#join shp and data.frame
total_especies_shp<-sp::merge(regiao, total_especies, by="regiao_cg")

output file

Since you are using {sf} anyhow, and sf package objects are modified data frames you can consider dplyr::left_join() to merge the two data frames.

Just be sure to start the pipe from your shapefile, as the output of a join inherits from the first object (and you want to end up with a sf object as output.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.