0

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")))
Spacedman
69.2k6 gold badges84 silver badges125 bronze badges
asked Apr 2, 2021 at 9:10
1
  • 2
    We don't have your data so we cant run your code and reproduce this. Can you make a simple example that shows the problem? Can you show us summaries of the input data? Can you take the mapply out of the equation and show the problem with just a call to exact_extract? Can you show which packages you used? Commented Apr 2, 2021 at 9:21

1 Answer 1

0

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".

Kadir Şahbaz
78.6k57 gold badges260 silver badges407 bronze badges
answered Apr 2, 2021 at 13:27
1
  • It would be great if you would mark your answer as the solution, this way it is clear to everyone at first sight. Commented Sep 7, 2024 at 18:20

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.