I have a list of a spatial dataset and a stack of rasters. The sf represent fires from 1985 to 2017 and the stack of rasters represent the climate conditions from 1985 to 2017. I want to extract the raster values for each pixel in the fire polygons using the exact_extract function in R. The function has the argument include_cols which is an optional character vector of column names in sf to be added to the data frame. Also, it has the argument append_cols that do the same when the fun is not NULL. However, when I try to apply the function I get the following error:
Error in `[.data.frame`(x, i, j, drop = drop) :
undefined columns selected
Here is my code:
waterclimPre_extract<- mapply(x= waterclimPre_maps, y = polys_maps, FUN= function(x,y)
exact_extract(x, y, include_xy = T, include_cell=T, include_cols = c("year", "fireClas")))
and also:
waterclimPre_stat <- mapply(x= waterclimPre_maps, y = polys_maps, FUN= function(x,y)
exact_extract(x, y, fun = c("mean", "min", "max", "median", "variance"), append_cols = c("year", "fireClas")))
1 Answer 1
The source of the error was that not all SF in the list had the same name in the columns. One, in particular, had "Year" instead of "year".
-
It would be great if you would mark your answer as the solution, this way it is clear to everyone at first sight.Pieter– Pieter2024年09月07日 18:20:19 +00:00Commented Sep 7, 2024 at 18:20
mapply
out of the equation and show the problem with just a call toexact_extract
? Can you show which packages you used?