How can I fix the issue with this for loop? [closed]

Solution 1:

You could test pi before trying to assign to percent_area. I'm not sure what st_intersection returns but from your comment, I'm guessing a data.frame. Assuming that, could try something such as

pi <- st_intersection(treatment, home.range.t)

if(NROWS(pi) != 0){
  home.range$percent_area <- as.numeric(st_area(pi) / st_area(home.range.t) *100)
} else {
  home.range$percent_area <- 0
}