'How to create a mosaic from different shapefile in R?

I have 37 shapefiles with different names.Individually each of them represent a different part of a country. I would like to create a mosaic of my 37 shp to just have one a the end. To make things clear I would them on one file. My filenames are similar and only one variable on it is changeable. I've tried this but it did not work :

date <- "20210901"
#----------------------------  


tileC <- c("T38JLT","T38JMT","T38LQK","T38LRJ",
           "T38LRK","T38LRL","T39KUB","T39LTE")

for (i in 1:length(tileC)) {
  
  name <- tileC[i]
  contour_shp <-  shapefile(glue("C:/Users/BIC/Desktop/{date}/S{date}_L_{name}_D/{date}_contour_mang_{name}.shp"))
  merge <- mosaic(contour_shp)
  #shapefile(merge,(glue("C:/Users/BIC/Desktop/{date}/{date}_contour_mang_madagascar.shp")))

}

Another thing is that they do not have the same SCR, could I merged them into one shapefile even if they do not have the same coordinate system ? If someone has a clue on what's wrong I am down for it !



Solution 1:[1]

one possibility:

library(sf)

outfile_name = "all-those-shapes.shp"
for(file_name in tileC){
    full_path = glue("C:/Users/BIC/Desktop/.../{file_name}.shp")

    read_sf(full_path) %>%
        ## if attributes differ between shapefiles, keep geometry only:
        st_geometry %>%
        st_transform(3857) %>% ## set common CRS (here: Google Mercator)
        write_sf(dsn = outfile_name,
                 append = TRUE ## add current feature as layer
                 )
}

If you don't need to reproject or drop attributes, there's even shorter solutions: combining multiple shapefiles in R

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 I_O