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

weighted.mean() crashes when weights are numeric and na.rm=TRUE #777

Closed
dholstius opened this issue Aug 23, 2022 · 1 comment
Closed

weighted.mean() crashes when weights are numeric and na.rm=TRUE #777

dholstius opened this issue Aug 23, 2022 · 1 comment

Comments

@dholstius
Copy link

Setup:

library(terra)
#> terra 1.6.7
x <- rast(nrows=10, ncols=10, xmin=0, xmax=10, vals = 1)
y <- rast(nrows=10, ncols=10, xmin=0, xmax=10, vals = 2)
y[1,1] <- NA
z <- c(x, y)

It's ok to use numeric weights if na.rm is not supplied:

wt <- c(0.1, 0.9)
weighted.mean(z, wt)
#> class       : SpatRaster 
#> dimensions  : 10, 10, 1  (nrow, ncol, nlyr)
#> resolution  : 1, 18  (x, y)
#> extent      : 0, 10, -90, 90  (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 
#> source      : memory 
#> name        : sum 
#> min value   : 1.9 
#> max value   : 1.9

Without the weights, but with na.rm = TRUE, there's an error:

weighted.mean(z, na.rm = TRUE)
#> Error in if (is.bool(i)) {: the condition has length > 1

The combination is deadly:

# this crashes
weighted.mean(z, wt, na.rm = TRUE) 

image

Created on 2022-08-23 by the reprex package (v2.0.1)

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.2.0 (2022-04-22)
#>  os       macOS Monterey 12.5.1
#>  system   aarch64, darwin20
#>  ui       X11
#>  language (EN)
#>  collate  en_US.UTF-8
#>  ctype    en_US.UTF-8
#>  tz       America/Los_Angeles
#>  date     2022-08-23
#>  pandoc   2.18 @ /Applications/RStudio.app/Contents/MacOS/quarto/bin/tools/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date (UTC) lib source
#>  cli           3.3.0   2022-04-25 [1] CRAN (R 4.2.0)
#>  codetools     0.2-18  2020-11-04 [1] CRAN (R 4.2.0)
#>  digest        0.6.29  2021-12-01 [1] CRAN (R 4.2.0)
#>  evaluate      0.15    2022-02-18 [1] CRAN (R 4.2.0)
#>  fastmap       1.1.0   2021-01-25 [1] CRAN (R 4.2.0)
#>  fs            1.5.2   2021-12-08 [1] CRAN (R 4.2.0)
#>  glue          1.6.2   2022-02-24 [1] CRAN (R 4.2.0)
#>  highr         0.9     2021-04-16 [1] CRAN (R 4.2.0)
#>  htmltools     0.5.2   2021-08-25 [1] CRAN (R 4.2.0)
#>  knitr         1.39    2022-04-26 [1] CRAN (R 4.2.0)
#>  magrittr      2.0.3   2022-03-30 [1] CRAN (R 4.2.0)
#>  Rcpp          1.0.8.3 2022-03-17 [1] CRAN (R 4.2.0)
#>  reprex        2.0.1   2021-08-05 [1] CRAN (R 4.2.0)
#>  rlang         1.0.3   2022-06-27 [1] CRAN (R 4.2.0)
#>  rmarkdown     2.14    2022-04-25 [1] CRAN (R 4.2.0)
#>  rstudioapi    0.13    2020-11-12 [1] CRAN (R 4.2.0)
#>  sessioninfo   1.2.2   2021-12-06 [1] CRAN (R 4.2.0)
#>  stringi       1.7.6   2021-11-29 [1] CRAN (R 4.2.0)
#>  stringr       1.4.0   2019-02-10 [1] CRAN (R 4.2.0)
#>  terra       * 1.6-7   2022-08-07 [1] CRAN (R 4.2.0)
#>  withr         2.5.0   2022-03-03 [1] CRAN (R 4.2.0)
#>  xfun          0.31    2022-05-10 [1] CRAN (R 4.2.0)
#>  yaml          2.3.5   2022-02-21 [1] CRAN (R 4.2.0)
#> 
#>  [1] /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────
@rhijmans
Copy link
Member

rhijmans commented Aug 24, 2022

Thank you very much, I now get:

weighted.mean(z, wt, na.rm = TRUE)
#class       : SpatRaster
#dimensions  : 10, 10, 1  (nrow, ncol, nlyr)
#resolution  : 1, 18  (x, y)
#extent      : 0, 10, -90, 90  (xmin, xmax, ymin, ymax)
#coord. ref. : lon/lat WGS 84
#source      : memory
#name        : lyr1
#min value   :  1.0
#max value   :  1.9

"terra" does not define a weighted.mean<SpatRaster,missing> method and the enigmatic error message you get with weighted.mean(z, na.rm = TRUE) comes from stats::weighted.mean. I think we can live with that.

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