I have sf data frame with refugee camps / populations across sub-Saharan Africa from 2009-2018. I created 50KM buffers around each camp for analysis, and am using that file for analysis buffer_shp
data <- data.frame(country = rep(c("Angola", "DRC", "Uganda"), each = 10),
location = rep(c("Luanda", "Kilueka", "Adjumani"), each = 10),
year = rep(2009:2018, times = 3),
population = sample(10000:50000, 30, replace = TRUE),
lon = c(13.23294531, 14.47044, 31.7905912),
lat = c(-8.815191647, -5.42059, 3.3769442))
data$lon[data$location == "Luanda"] <- 13.23294531
data$lat[data$location == "Luanda"] <- -8.815191647
data$lon[data$location == "Kilueka"] <- 14.47044
data$lat[data$location == "Kilueka"] <- -5.42059
data$lon[data$location == "Adjumani"] <- 31.7905912
data$lat[data$location == "Adjumani"] <- 3.3769442
data_sf <- st_as_sf(data, coords = c("lon", "lat"), crs = 4326)
buffer_shp <- data_sf %>%
st_transform("ESRI:102022") %>%
st_buffer(dist = 100000)
## I make an extra buffer item, so that I don't have to keep creating the original if there are mistakes
buffer_shp2 <- buffer_shp %>%
mutate(ID = seq_len(nrow(.)))
I also have 10 raster files showing rainfall per year. I am trying to find the mean value of the cells that intersect the polygon for each location, every year. I want to create a new column in buffer_shp2
called 'CHIRPS_mean' that tracks the resulting mean value for the intersection. I have tried so many different things and I just cannot get it to work.
Iteration 1:
# List with all my rasters (Could this be a stack?)
CHIRPS_annual_list <- list(CHIRPS_annual_2009, CHIRPS_annual_2010,
CHIRPS_annual_2011, CHIRPS_annual_2012,
CHIRPS_annual_2013, CHIRPS_annual_2014,
CHIRPS_annual_2015, CHIRPS_annual_2016,
CHIRPS_annual_2017, CHIRPS_annual_2018)
years <- 2009:2018
for (year in 2009:2018){
# Get the corresponding raster
raster_name <- paste0("CHIRPS_annual_", year)
raster_brick <- get(raster_name)
raster_brick[raster_brick == -9999] <- NA
# Filter buffer shapefile to the current year
buffer_year <- buffer_shp2 %>%
dplyr::filter(Year == year)
# Empty dataframe to store values
intersection_df <- data.frame(ID = numeric(), Year = numeric(), CHIRPS_mean = numeric())
# Loop through each row in the buffer shapefile, finding intersection
for (i in 1:nrow(buffer_year)) {
buffer_row <- buffer_year[i,]
intersection <- extract(raster_brick, buffer_row, fun = mean, na.rm = TRUE)
new_row <- data.frame(ID = buffer_row$ID,
Year = year,
CHIRPS_mean = intersection)
# Add the intersection value to the dataframe
intersection_df <- rbind(intersection_df, new_row)
buffer_shp2 <- left_join(buffer_shp2, intersection_df, by = c("ID", "Year"))
}
This results in something along the lines of the following, it seems to create a new column for each year and location.
output = CHIRPS_mean.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y. ...
CHIRPS_mean.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x. ...
CHIRPS_mean.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y. ...
CHIRPS_mean.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x. ...
If I move the rbind and left_join outside the loop, it then doesn't do it for every year. I seem to be able to figure it out if it is just one year, but I am not sure how to process through the for-loop. Any help would be appreciated.
I have spent hours on this and am incredibly stuck. Thank you in advance.