Extracting poly.counts through loops

by Yasha   Last Updated June 30, 2020 01:22 AM - source

What I want to do

I want to extract the count of points over Africa at the first administrative district level at monthly intervals over 26 years. My point data consist of conflict events. I am struggling to figure out how to do this without spending a lifetime copy-pasting code.

I need to capture three things: the country, administrative district name, and the count of events.

My intuition is to use poly.counts() from GISTools in some sort of a loop, such as:

for(y in 1990:2016){
    for(m in 1:12){
       poly.counts(subset(data, year == y & month == m))

I'm not sure how to capture the output of each iteration into a single data frame. Ideally, this will produce a single data frame that shows the year, month, country, administrative district, and count. Below, I present a short "toy" version of what I want.

year     month     country     admin1     count
----     -----     --------    -------    ------
2003     1         Nigeria     Lagos      67
2003     1         Nigeria     Oyo        12
2003     1         Nigeria     Kaduna     54
2003     2         Nigeria     Lagos      12
2003     2         Nigeria     Oyo        24
2003     2         Nigeria     Kaduna     56
2003     3         Nigeria     Lagos      15
2003     3         Nigeria     Oyo        22
2003     3         Nigeria     Kaduna     14

Any help would be appreciated! I did a search, but did not see any duplicate questions.

Tags : r loop

Related Questions

Looping script with Zonal Statistics

Updated February 06, 2018 20:22 PM

How to use a loop to get coordinates in GEE

Updated January 17, 2018 08:22 AM

cursor reset does not work in UpdateCursor

Updated June 09, 2017 15:22 PM