How can I create a for loop for this code?
The code below accomplishes my goal of calculating the percentage of a home-range overlapping my treatment area. However, I have 190 home-ranges. Any advice on creating a for loop or other methods to automate this process for all home-ranges? Thank you
I do not know how to create a reproducible example in this situation, but I will if anyone can guide me through the process.
library(sf)
library(plyr)
library(amt)
library(raster)
library(mapview)
######################################################
#read in treatment area shapefile
treatment <- st_read('C:\\Users\\kujld016\\Desktop\\All\\Projects\\Brush_Management\\imagery\\Treatment_perimeter.shp')
#check projection
projection(treatment)
#transform treatment shapefile to correct projection
treatment<-st_transform(treatment,'+proj=utm +zone=14 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs')
projection(treatment)
#######################################################
#read in home-range shapefile
home-range <- st_read("C:\\Users\\kujld016\\Desktop\\All\\Projects\\Brush_Management\\KingR_BBM\\contour_95_151_880_25_2 2009-08.shp")
#check projection
projection(home-range)
#transform home-range shapefile projection to match treatment area shapefile
home-range <- st_transform(home-range,'+proj=utm +zone=14 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs')
projection(home-range)
###################################################
#calculate area overlap between home-range shapefile and treatment area shapefile
pi <- st_intersection(treatment,home-range)
st_area(pi)
#calculate percentage of home-range shapefile overlapping treatment area shapefile and add value to new column
home-range$percent_area <- st_area(pi) / st_area(home-range)```
You can modify your script to loop through multiple home ranges like so:
First define your treatment
as you have done, assuming that it is the same treatment for each home range:
treatment <- st_read('C:\\Users\\kujld016\\Desktop\\All\\Projects\\Brush_Management\\imagery\\Treatment_perimeter.shp')
treatment <- st_transform(treatment,'+proj=utm +zone=14 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs')
Next, create a list of the 190 shapefile names using list.files()
. For this, put all 190 shapefiles into the same folder, ideally somewhere within your parent directory. The path
argument in list.files()
will point to where. "*.shp"
will include any file with the .shp extension.
home.names <- list.files(path = "foldername", pattern = "*.shp")
Now write the loop, which will iterate through each shapefile.
all.home.ranges <- list()
for (shp in 1:length(home.names)) {
home.range <- st_read(home.names[shp])
home.range.t <- st_transform(home.range,'+proj=utm +zone=14 +ellps=GRS80
+towgs84=0,0,0,0,0,0,0 +units=m +no_defs')
pi <- st_intersection(treatment, home.range.t)
home.range.t$percent_area <- st_area(pi) / st_area(home.range.t)
all.home.ranges[[shp]] <- home.range.t
}
all.home.ranges
will be a list with 190 elements, each of which is a different home range.
Note that this loop will likely take awhile to run. I would recommend testing it by changing for (shp in 1:length(home.names))
to for (shp in 1:length(home.names[1:2]))
, which will only do two iterations.