3

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?

1
  • 1
    What do you mean by "sf files"? In R you read data from things like shapefiles or geopackages into "sf objects". Do you have lots of sf objects you want to convert? Are you reading them from lots of shapefiles etc? How are they structured? Or is it the full contents of a folder?
    – Spacedman
    Commented Feb 14, 2022 at 23:38

1 Answer 1

6

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))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.