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?