I have several sf files that have different CRS. Normally I would use the following code and "copy paste" it several times for each file.
st_geometry(Zonas) = st_transform(st_geometry(Zonas), 4326)
Is there a way to do this on a loop?
I have several sf files that have different CRS. Normally I would use the following code and "copy paste" it several times for each file.
st_geometry(Zonas) = st_transform(st_geometry(Zonas), 4326)
Is there a way to do this on a loop?
You can make use of list.files
and lapply
to read all the files into a list and then apply the same transformation to each entry inside the list.
library(sf)
# Supposing the files are .shp files, list all files that match the desired pattern inside the folder of interest.
files <- list.files("folder",
"\\.shp$",
full.names = T)
# Read all files into a list
list_sf <- lapply(files, st_read)
# Apply same transformation to every entry in the list
list_sf_trans <- lapply(list_sf, function(x) st_transform(x, 4326))