0

I started off by creating two subsets of my data frame like so:

a = read_sf("a.shp")
a = st_make_valid(a)

#creating first subset with polygons from base year 2000
a1 = subset(a, year==2000)

#creating second subset with polygons from all the following years
a2 = subset(a, year>2000)

#creating buffer around base year
buffer = st_buffer(a1, 500)

#intersection of buffer from base year with polygons from the following years 
#(lets assume the column "year" in the data.frame has a range of years 2000-2010)
results2000 = st_intersection(buffer, a2)

I now have to do those steps for every following year till year 2010. So the next results would look like this:

a = read_sf("a.shp")
a = st_make_valid(a)

#creating first subset with polygons from base year 2000
a1 = subset(a, year==2001)

#creating second subset with polygons from all the following years
a2 = subset(a, year>2001)

#creating buffer around base year
buffer = st_buffer(a1, 500)

#intersection of buffer from base year with polygons from the following years 
#(lets assume the column "year" in the data.frame has a range of years 2000-2010)
results2001 = st_intersection(buffer, a2)

The only entries that change are the years in the subset code.

In the end, I need one data.frame containing all 10 results (combining results2000 till results2010, each having one of the 10 years between 2000 and 2010 as their base year).

I could create all 10 results and combine them by

rbind(results2000,results2001,...) 

and so on.

But is their an easier way of doing so? I thought about using the foreach function from the foreach package, maybe something like this

(Spatial) Efficient way of finding all points within X meters of a point?

But creating a loop with foreach leads to a very long plotting time, as the data.frame "a" contains around 1 million rows.

Can anyone help?

1 Answers1

0

I don't know which package you use to get the data, so I am going to answer assuming that the output of st_intersection(buffer, a2) is a dataframe (because you say you want to use rbind).

If that's the case, you could create an empty list (that I called outlist below), then fill up that list in a loop, and finally, rbind the different element of the list together.

See code below:

a = read_sf("a.shp")
a = st_make_valid(a)


# Define your cutting year 
subset_yr <- 2000:2010
# Create an empty list you will populate
outlist <- vector(mode = "list", length = length(subset_yr))

# Your code in a loop
for(i in seq_along(subset_yr)) {
  #creating first subset with polygons from base year i (2000, 2001, etc.)
  a1 = subset(a, year==subset_yr[i])
  
  #creating second subset with polygons from all the following years
  a2 = subset(a, year>subset_yr[i])
  
  #creating buffer around base year
  buffer = st_buffer(a1, 500)
  
  #intersection of buffer from base year with polygons from the following years 
  outlist[[i]] = st_intersection(buffer, a2)
  
  #Add a column indicating the year
  outlist[[i]]$Year <- subset_yr[i]
}

# Bind everything together
rbind(outlist)
Dharman
  • 30,962
  • 25
  • 85
  • 135
Rosalie Bruel
  • 1,423
  • 1
  • 10
  • 22
  • `rbind(outlist) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] outlist sf,36 sf,36 sf,36 sf,36 sf,36 sf,36 sf,36 sf,36 sf,36 sf,36 sf,36` this is the output from your code, however I need a data.frame which has the original column names (year, area of intersection, ...) I tried `outlist<-data.frame()` instead of the vector and I deleted the added column for year (because it adds 11 columns for each base year) but I get the following error `Error in `[[<-.data.frame`(`*tmp*`, i, value = list(id = c(1, 2, : replacement has 12994 rows, data has 0` – Karla Keks Aug 13 '21 at 14:59
  • What package do you use to do read_sf("a.shp"), and is it a reproducible example? It would be easier to help if I saw the structure of st_intersection(buffer, a2). You could also write a code below with: ```results2000 = outlist[[1]]; results2001 = outlist[[2]]``` etc. – Rosalie Bruel Aug 13 '21 at 15:28
  • I am using the sf package but with your tip `outlist[[1]]` it generates the desired output for this subset and then setting ´results2000=outlist[[1]];...` and `rbind(results2000,results2001,...)` leads to the outpit I was hoping for! Thank you so much!!! – Karla Keks Aug 13 '21 at 16:03