Reputation: 33
I have used raster package (raster::extract) to calculate mean based on own function called Mean_condition (See below) and it works.
I would like to try using exactextractr::exact_extract to extract the same as I learned that it is more efficient and finishes quickly. However, it throwing me an error which I dont understand exactly. Appreciate if anyone help me on this error
Mean_condition <- function(x,...) {
if ((length(x[is.na(x)])/length(x)) >= 0.5)
{
return(9999)
}
else
return(mean(x))
}
Region1_mean <- raster::extract(raster, Regionshp, fun=Mean_condition, na.rm=TRUE, df=TRUE)
Using exactextractr:
Region1_mean <- exactextractr::exact_extract(raster, Regionshp, fun=Mean_condition, na.rm=TRUE)
Error in .exact_extract(x, sf::st_as_sf(y), ...) :
exact_extract was called with a function that does not appear to be of the form function(values, coverage_fractions, ...)
. If the summary function should accept a single data frame argument, set summarize_df = TRUE
.
In addition: Warning message:
In .exact_extract(x, sf::st_as_sf(y), ...) :
Polygons transformed to raster CRS (EPSG:NA)
Upvotes: 0
Views: 798
Reputation: 2584
What about something like this:
library(raster)
library(sf)
library(exactextractr)
rast <- raster::raster(matrix(1:100, ncol=10), xmn=0, ymn=0, xmx=10, ymx=10)
poly <- sf::st_as_sfc('POLYGON ((2 2, 7 6, 4 9, 2 2))')
Mean_condition <- function(rast,poly) {
if ((length(x[is.na(x)]) / length(x)) >= 0.5){
return(9999)
}
else{
Region1_mean <- exactextractr::exact_extract(rast, poly, fun="mean")
}
return(Region1_mean)
}
Mean_condition(rast,poly)
since you have to calculate a mean, you should rely on the more efficient built-in summary operations (such as mean
) instead of building a user-defined function, which is slower. from the function's description:
Where possible, this approach [using a predefined summary operation] is advantageous because it allows the package to calculate the statistics incrementally, avoiding the need to store all pixel values in memory at the same time. This allows the package to process arbitrarily large data with a small amount of memory.
Also, note that your error is due to the missing of the argument summarize_df = TRUE.
Again from the function description:
The simplest way to write a summary function is to set argument summarize_df = TRUE. (For backward compatibility, this is not the default.) In this mode, the summary function takes the signature function(df, ...) where df is the same data frame that would be returned by exact_extract with fun = NULL.
With summarize_df = FALSE, the function must have the signature function(values, coverage_fractions, ...) when weights are not used
Upvotes: 0