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
NA replaced by 0 when na.rm = TRUE in weighted.mean #574
Comments
Thanks. The sum of weights was not adjusted. So you with your example the value returned became |
Thank you for the fix and for the explanation! |
Looks like I was too fast. I'm still seeing the same behavior with the latest dev version. library(terra)
#> terra 1.5.24
x <- rast(nrows=10, ncols=10, xmin=0, xmax=10, vals = 1)
y <- rast(nrows=10, ncols=10, xmin=0, xmax=10, vals = 2)
z <- rast(nrows=10, ncols=10, xmin=0, xmax=10, vals = 3)
wt <- c(x,y,z)
z[1,1] <- NA
xt <- c(x,y,z)
weighted.mean(xt, 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 : sum
#> min value : 0.8333333
#> max value : 2.333333
# The minimum is equivalent to:
weighted.mean(c(1,2,0), 1:3)
#> [1] 0.8333333
# But I expected it to be:
weighted.mean(x = c(1,2,NA), w = 1:3, na.rm = TRUE)
#> [1] 1.666667 Created on 2022-03-28 by the reprex package (v2.0.1) Session infosessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#> setting value
#> version R version 4.1.3 (2022-03-10)
#> os Ubuntu 20.04.4 LTS
#> system x86_64, linux-gnu
#> ui X11
#> language en_US:en
#> collate en_US.UTF-8
#> ctype en_US.UTF-8
#> tz Europe/Copenhagen
#> date 2022-03-28
#> pandoc 2.11.4 @ /usr/lib/rstudio/bin/pandoc/ (via rmarkdown)
#>
#> ─ Packages ───────────────────────────────────────────────────────────────────
#> package * version date (UTC) lib source
#> cli 3.2.0 2022-02-14 [1] CRAN (R 4.1.2)
#> codetools 0.2-18 2020-11-04 [4] CRAN (R 4.0.3)
#> crayon 1.5.0 2022-02-14 [3] CRAN (R 4.1.2)
#> digest 0.6.29 2021-12-01 [1] CRAN (R 4.1.2)
#> ellipsis 0.3.2 2021-04-29 [3] CRAN (R 4.0.5)
#> evaluate 0.15 2022-02-18 [3] CRAN (R 4.1.2)
#> fansi 1.0.3 2022-03-24 [1] CRAN (R 4.1.3)
#> fastmap 1.1.0 2021-01-25 [3] CRAN (R 4.0.3)
#> fs 1.5.2 2021-12-08 [1] CRAN (R 4.1.2)
#> glue 1.6.2 2022-02-24 [1] CRAN (R 4.1.2)
#> highr 0.9 2021-04-16 [3] CRAN (R 4.0.5)
#> htmltools 0.5.2 2021-08-25 [1] CRAN (R 4.1.1)
#> knitr 1.38 2022-03-25 [3] CRAN (R 4.1.3)
#> lifecycle 1.0.1 2021-09-24 [1] CRAN (R 4.1.1)
#> magrittr 2.0.2 2022-01-26 [1] CRAN (R 4.1.2)
#> pillar 1.7.0 2022-02-01 [1] CRAN (R 4.1.2)
#> pkgconfig 2.0.3 2019-09-22 [3] CRAN (R 4.0.0)
#> purrr 0.3.4 2020-04-17 [3] CRAN (R 4.0.0)
#> R.cache 0.15.0 2021-04-30 [1] CRAN (R 4.1.0)
#> R.methodsS3 1.8.1 2020-08-26 [1] CRAN (R 4.1.0)
#> R.oo 1.24.0 2020-08-26 [1] CRAN (R 4.1.0)
#> R.utils 2.11.0 2021-09-26 [1] CRAN (R 4.1.1)
#> Rcpp 1.0.8.3 2022-03-17 [1] CRAN (R 4.1.3)
#> reprex 2.0.1 2021-08-05 [1] CRAN (R 4.1.0)
#> rlang 1.0.2 2022-03-04 [1] CRAN (R 4.1.2)
#> rmarkdown 2.13 2022-03-10 [1] CRAN (R 4.1.2)
#> rstudioapi 0.13 2020-11-12 [3] CRAN (R 4.0.3)
#> sessioninfo 1.2.2 2021-12-06 [3] CRAN (R 4.1.2)
#> stringi 1.7.6 2021-11-29 [1] CRAN (R 4.1.2)
#> stringr 1.4.0 2019-02-10 [3] CRAN (R 4.0.0)
#> styler 1.7.0 2022-03-13 [1] CRAN (R 4.1.3)
#> terra * 1.5-24 2022-03-28 [1] Github (rspatial/terra@57d0df0)
#> tibble 3.1.6 2021-11-07 [1] CRAN (R 4.1.2)
#> utf8 1.2.2 2021-07-24 [3] CRAN (R 4.1.0)
#> vctrs 0.3.8 2021-04-29 [3] CRAN (R 4.0.5)
#> withr 2.5.0 2022-03-03 [1] CRAN (R 4.1.2)
#> xfun 0.30 2022-03-02 [1] CRAN (R 4.1.2)
#> yaml 2.3.5 2022-02-21 [1] CRAN (R 4.1.2)
#>
#> [1] /home/au206907/R/x86_64-pc-linux-gnu-library/4.1
#> [2] /usr/local/lib/R/site-library
#> [3] /usr/lib/R/site-library
#> [4] /usr/lib/R/library
#>
#> ────────────────────────────────────────────────────────────────────────────── |
Very strange, but I think this is fixed now. It seems I only changed the version where |
Title pretty much says it, but it looks like NA are being interpreted as 0 in
weighted.mean
whenna.rm = TRUE
I get this on OSX with terra 1.5.21 and on Ubuntu with latest dev version of terra.
Created on 2022-03-17 by the reprex package (v2.0.1)
The text was updated successfully, but these errors were encountered: