Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zonal removing NA/NaN values from x when using custom fun #1133

Closed
mwip opened this issue Apr 27, 2023 · 2 comments
Closed

zonal removing NA/NaN values from x when using custom fun #1133

mwip opened this issue Apr 27, 2023 · 2 comments

Comments

@mwip
Copy link

mwip commented Apr 27, 2023

Problem

When using zonal on a raster x that contains NA or NaN values, these are omitted prior to passing x into in custom functions. This does not happen when using built-in functions like fun = "isNA".

Expected behavior

This, to me, is unexpected behavior, as I would expect the vector passed to the custom function to include NA and NaN.

Example

See this minimal reproducible example:

library(terra)

values_rast <- rast(matrix(c(1, 2, 3, NA), ncol = 2))
names(values_rast) <- "values"

zones_rast <- rast(matrix(c(1, 1, 2, 2), ncol = 2))
names(zones_rast) <- "zones"

plot(c(values_rast, zones_rast), colNA = "red")

zonal(x = values_rast, z = zones_rast, function(x) {
  print(x)
  return(sum(is.na(x)))
})

# [1] 1 2
# [1] 3     # <------ unexpected, should be c(3, NA)
#   zones values
# 1     1      0
# 2     2      0  # <------ unexpected, should be 1

zonal(x = values_rast, z = zones_rast, fun = "isNA")
#   zones values
# 1     1      0
# 2     2      1  # <---------- expected

System details

OS/Kernel: Linux 5.15, Ubuntu 20.04, x86_64,
Tested on terra version 1.7.29 (latest version on CRAN) and 1.7.30 (via devtools::install_github("rspatial/terra"))

The issue was reproducible on a Windows machine, too.

@mwip mwip changed the title Zonal removing NA/NaN values from x when using custom fun zonal removing NA/NaN values from x when using custom fun Apr 27, 2023
@rhijmans
Copy link
Member

Thanks, I now get:

library(terra)
v <- rast(matrix(c(1, 2, 3, NA), ncol = 2))
names(v) <- "values"
z <- rast(matrix(c(1, 1, 2, 2), ncol = 2))
names(z) <- "zones"

zonal(v, z, \(x) sum(is.na(x)))
#  zones values
#1     1      0
#2     2      1

zonal(v, z, "isNA")
#  zones values
#1     1      0
#2     2      1


zonal(v, z, \(x) mean(x))
#  zones values
#1     1    1.5
#2     2     NA

zonal(v, z, "mean")
#  zones values
#1     1    1.5
#2     2     NA


zonal(v, z, \(x) mean(x, na.rm=T))
#  zones values
#1     1    1.5
#2     2    3.0

zonal(v, z, \(x, ...) mean(x, ...), na.rm=T)
#  zones values
#1     1    1.5
#2     2    3.0

zonal(v, z, "mean", na.rm=T)
#  zones values
#1     1    1.5
#2     2    3.0

@mwip
Copy link
Author

mwip commented Apr 28, 2023

I can confirm this fixed the issue. Thank you for taking the time and resolving this that quickly. I really appreciate your dedication!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants