Overview
Using R, I would like to count the number of points within a polygon according to a specific criterion (a temporal window).
I have the following data:
- Geo-located survey data that include the date of the survey interview. Thus, I am able to pinpoint exactly when and where each survey was conducted and to map them out across the United States.
- Geo-located data about political rallies across the United States. These also include the date.
Using QGIS, I created a set of circular 50 mile buffers around each survey respondent. My goal is to count the number of political rallies that falls within each respondent's "buffer" within a specific time frame preceding the interview. The 50 mile buffers created in QGIS retain all variables of the original data, including the date of the interview.
Data
Using QGIS, I created some mock shapefiles with dates and locations to aid in replication.
Approach
I am trying to use GISTools::poly.counts to count the number of rallies within different temporal windows (30 days, 90 days, etc.).
Generally, to count the number of points within a polygon, I would simply use:
count <- GISTools::poly.counts(rallies, buffer)
This gives me the total number of rallies that occur within each buffer, but doesn't allow me to specify temporal windows. For example, it would be great to develop a count of the number of rallies within a buffer for the 30 days preceding the survey interview as well as the 90 days preceding the interview.
Remember, each polygon within my buffer shapefile has a different date of interview.
Here's what I've tried, but it's not working:
buffer$count_30 <- GISTools::poly.counts(
rallies[buffer$date - rallies$date > 0 & buffer$date - rallies$date <= 30],
buffer)
I get the following error:
Error in `[.data.frame`(x@data, i, j, ..., drop = FALSE) :
undefined columns selected
In addition: Warning messages:
1: In unclass(time1) - unclass(time2) :
longer object length is not a multiple of shorter object length
2: In unclass(time1) - unclass(time2) :
longer object length is not a multiple of shorter object length
What is the correct way to accomplish this?

