I am working with shapefiles where the location id data is only available in the folder name. I wrote the following code to read the files in as a batch, but I am struggling with how to include the location id folder name in a column as well.
folders <- list.dirs(path = "/home/username/foldername/location_id",
full.names = TRUE, recursive = TRUE)
all_files <- function(folder) {
out<-readOGR(dsn = paste0(folder), layer = "SAMENAME", verbose = TRUE)
return(out)
}
my_shapefiles <- purrr::map(folders, safely(all_files))
Two approaches:
folder <- "/home/username/foldername/folder2"
basename(folder)
#> [1] "folder2"
gsub(".*\\/(.*)$", "\\1", folder)
#> [1] "folder2"
Therefore, you could modify your function like this:
all_files <- function(folder) {
out <- readOGR(dsn = paste0(folder), layer = "SAMENAME", verbose = TRUE)
out$id <- basename(folder)
return(out)
}
Created on 2021-10-18 by the reprex package (v2.0.1)