Tiffany Rae Duhl
Tiffany Rae Duhl

Reputation: 1

Loop through csv files in folder and perform spatial intersect

though I've seen similar questions here about looping through and performing operations on csv files within a directory, the code I've written for my particular case isn't working.

I have ~100 csv files in a directory, which are spatial datasets (all have identical columns including lat/lon coordinates and other data, but different #'s of rows). I'm trying to clip each of them using a bounding box. Here's what I have so far:

library(sf)

setwd("C:\\mydirectory\\")

filelist<- list.files(pattern = ".csv") #make file list with all .csv files in directory

lapply(filelist, function(x) {
read.table(x, header=T) %>%
st_as_sf(coords = c("LON", "LAT"), crs = 4326, remove = F) %>%  #convert to spatial dataframe object
st_transform(crs = 32619) %>%   #I need this to be in units of m so I re-project to UTM 19N
e <- st_as_sfc(st_bbox(c(xmin = 331380, xmax = 338080, 
           ymax = 4698700, ymin = 4691000), 
           crs = st_crs(32619)))   #e is my spatial clipping extent
      
pts_sub <- st_intersection(x, e) %>%    #clip my sf object
write.csv(paste0("new_", x), row.names = FALSE)   #export as another csv file with slightly different 
})                                                    #name

The first problem I encounter is "Error in [.data.frame(x, coords) : undefined columns selected" when trying to pass x into the st_as_sf function. Here's an example of one of the csv files

>data<- read.csv("example.csv")
>data
    Date     Time      LAT       LON Leg.Speed  Conc 
62 1/14/2021 11:32:34 42.41039 -71.12059   0.7 mph 10100
63 1/14/2021 11:32:35 42.41039 -71.12058   1.0 mph 10100 
64 1/14/2021 11:32:36 42.41038 -71.12058   1.4 mph 10100 
65 1/14/2021 11:32:37 42.41038 -71.12057     2 mph 10200 
66 1/14/2021 11:32:38 42.41037 -71.12057     2 mph 10300 
67 1/14/2021 11:32:39 42.41036 -71.12057   1.5 mph 10300 
68 1/14/2021 11:32:40 42.41036 -71.12056   1.0 mph  9980 
69 1/14/2021 11:32:41 42.41036 -71.12056   0.7 mph 10000 

Thanks anyone out there who can see my mistake!

Upvotes: 0

Views: 53

Answers (1)

Tiffany Rae Duhl
Tiffany Rae Duhl

Reputation: 1

Thanks to Robert's suggestion, I'm now successfully using the following loop:

''' library(dplyr) library(sf)

setwd("C:\\mypath\\")

filelist<- list.files(pattern = ".csv") #make file list with all .csv files in 
directory

for(i in filelist){
name<- substr(i, 1, 9) 
df1<- read.csv(i, header = TRUE)
df2<- st_as_sf(df1, coords = c("LON", "LAT"), crs = 4326, remove = F)
df3<- st_transform(df2, crs = 32619)  
e <- st_as_sfc(st_bbox(c(xmin = 331380, xmax = 338080, 
           ymax = 4698700, ymin = 4691000), 
           crs = st_crs(32619)))
      
pts_sub <- st_intersection(df3, e)
#write.csv(paste0("new_", pts_sub), row.names = FALSE)
write.csv(pts_sub, file = paste0((name), "_clip.csv"),  row.names = FALSE)
}

'''

Upvotes: 0

Related Questions