Search code examples
rforeachspatial

automize combining data frames in R


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?


Solution

  • 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)