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

aggregate.stars mean by time (by = "2 days") #679

Closed
alexyshr opened this issue Apr 15, 2024 · 2 comments
Closed

aggregate.stars mean by time (by = "2 days") #679

alexyshr opened this issue Apr 15, 2024 · 2 comments

Comments

@alexyshr
Copy link

alexyshr commented Apr 15, 2024

Before using aggregate.stars the values in the original stars object pointsstars1 for 6 days are 0, 0, 0, 0, 0, 0. After aggregating by 2 days using mean the values should be 0, 0, 0 , but they are 0, 1, NaN (stars object pointstarsagg1). In the plot it can be seen that the mean function is returning values for some cells in some aggregated dates (the point appears in the plot), and NA values for the same cells in other aggregated dates (the point disappears in the plot); as the cell values come from the same original stars object pointsstars the aggregation must return all NA or all a value. In other cases the mean value above the original repeated value. Is it a mistake on my side?

library(dplyr)
#> Warning: package 'dplyr' was built under R version 4.3.3
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(sf)
#> Warning: package 'sf' was built under R version 4.3.3
#> Linking to GEOS 3.11.2, GDAL 3.8.2, PROJ 9.3.1; sf_use_s2() is TRUE
library(stars)
#> Warning: package 'stars' was built under R version 4.3.3
#> Loading required package: abind
library(ggplot2)

##################################
###create points
#x and y sequences with names
x <- seq(from = 0, to = 10, by = 1)
y <- seq(from = 10, to = 0, by = -1)
xy <- expand.grid(x = x, y = y)
xy$u <- x

#randomly select 20% of the u values and set them to NA
#using ifelse, runif and dplyr
the_condition <- runif(nrow(xy)) < 0.2
xy <- xy %>%
  dplyr::mutate(u = ifelse(the_condition, NA, u))

#convert the dataframe xy to sf
points <- sf::st_as_sf(xy, coords = c("x", "y"), crs = 4326)

#convert points to stars
pointsstars <- stars::st_as_stars(points)

#create an stars object with 6 dates one per day
pointsstars <- c(pointsstars, pointsstars, pointsstars, 
                 pointsstars, pointsstars, pointsstars)


#convert attributes to dimensions
pointsstars <- merge(pointsstars)

#change the name of the dimensions
pointsstars <- st_set_dimensions(pointsstars, names = c("geometry", "time"))

#create a date sequence with 6 dates
the_dates <- seq.Date(Sys.Date(), by = 1, length.out = 6)

#set time dimension values
pointsstars <- pointsstars %>% st_set_dimensions(which = "time", values = the_dates)
names(pointsstars) <- "u"
pointsstars
#> stars object with 2 dimensions and 1 attribute
#> attribute(s):
#>    Min. 1st Qu. Median     Mean 3rd Qu. Max. NA's
#> u     0       2      5 4.777778       8   10  186
#> dimension(s):
#>          from  to     offset  delta refsys point                        values
#> geometry    1 121         NA     NA WGS 84  TRUE POINT (0 10),...,POINT (10 0)
#> time        1   6 2024-04-15 1 days   Date    NA                          NULL

#select first polygon
pointsstars1 <- pointsstars[ , 1, ]

#remove unused dimensions
pointsstars1 <- pointsstars1 %>% adrop
pointsstars1[[1]]
#> [1] 0 0 0 0 0 0

#aggregate by time (2 days window)
pointstarsagg <- aggregate(
  x = pointsstars,
  by = "2 days",
  FUN = mean,
  na.rm = TRUE
)

#change order of dimensions
pointstarsagg <- pointstarsagg %>% aperm(c(2,1))
pointstarsagg
#> stars object with 2 dimensions and 1 attribute
#> attribute(s):
#>    Min. 1st Qu. Median     Mean 3rd Qu. Max. NA's
#> u     0       2      5 4.777778       8   10   93
#> dimension(s):
#>          from  to     offset  delta refsys point                        values
#> geometry    1 121         NA     NA WGS 84  TRUE POINT (0 10),...,POINT (10 0)
#> time        1   3 2024-04-15 2 days   Date    NA                          NULL

#extract the value for the first polygon
pointstarsagg1 <- pointstarsagg[ , 1, ]
pointstarsagg1[[1]]
#>      [,1] [,2] [,3]
#> [1,]    0    1  NaN
plot(pointstarsagg)

Created on 2024-04-15 with reprex v2.0.2

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.3.2 (2023-10-31 ucrt)
#>  os       Windows 10 x64 (build 19045)
#>  system   x86_64, mingw32
#>  ui       RTerm
#>  language (EN)
#>  collate  English_United States.utf8
#>  ctype    English_United States.utf8
#>  tz       America/New_York
#>  date     2024-04-15
#>  pandoc   3.1.1 @ C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date (UTC) lib source
#>  abind       * 1.4-5   2016-07-21 [1] CRAN (R 4.3.0)
#>  class         7.3-22  2023-05-03 [2] CRAN (R 4.3.2)
#>  classInt      0.4-10  2023-09-05 [1] CRAN (R 4.3.1)
#>  cli           3.6.2   2023-12-11 [2] CRAN (R 4.3.3)
#>  colorspace    2.1-0   2023-01-23 [1] CRAN (R 4.3.1)
#>  curl          5.2.1   2024-03-01 [2] CRAN (R 4.3.3)
#>  DBI           1.2.2   2024-02-16 [1] CRAN (R 4.3.3)
#>  digest        0.6.33  2023-07-07 [1] CRAN (R 4.3.1)
#>  dplyr       * 1.1.4   2023-11-17 [1] CRAN (R 4.3.3)
#>  e1071         1.7-14  2023-12-06 [2] CRAN (R 4.3.3)
#>  evaluate      0.21    2023-05-05 [1] CRAN (R 4.3.1)
#>  fansi         1.0.6   2023-12-08 [1] CRAN (R 4.3.3)
#>  fastmap       1.1.1   2023-02-24 [1] CRAN (R 4.3.1)
#>  fs            1.6.3   2023-07-20 [1] CRAN (R 4.3.1)
#>  generics      0.1.3   2022-07-05 [1] CRAN (R 4.3.1)
#>  ggplot2     * 3.4.3   2023-08-14 [1] CRAN (R 4.3.1)
#>  glue          1.7.0   2024-01-09 [2] CRAN (R 4.3.3)
#>  gtable        0.3.4   2023-08-21 [1] CRAN (R 4.3.1)
#>  htmltools     0.5.6   2023-08-10 [1] CRAN (R 4.3.1)
#>  KernSmooth    2.23-22 2023-07-10 [1] CRAN (R 4.3.1)
#>  knitr         1.44    2023-09-11 [1] CRAN (R 4.3.1)
#>  lifecycle     1.0.4   2023-11-07 [1] CRAN (R 4.3.3)
#>  magrittr      2.0.3   2022-03-30 [1] CRAN (R 4.3.1)
#>  munsell       0.5.0   2018-06-12 [1] CRAN (R 4.3.1)
#>  pillar        1.9.0   2023-03-22 [1] CRAN (R 4.3.1)
#>  pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.3.1)
#>  proxy         0.4-27  2022-06-09 [1] CRAN (R 4.3.1)
#>  purrr         1.0.2   2023-08-10 [1] CRAN (R 4.3.1)
#>  R.cache       0.16.0  2022-07-21 [1] CRAN (R 4.3.2)
#>  R.methodsS3   1.8.2   2022-06-13 [1] CRAN (R 4.3.1)
#>  R.oo          1.26.0  2024-01-24 [1] CRAN (R 4.3.2)
#>  R.utils       2.12.3  2023-11-18 [1] CRAN (R 4.3.2)
#>  R6            2.5.1   2021-08-19 [1] CRAN (R 4.3.1)
#>  Rcpp          1.0.12  2024-01-09 [1] CRAN (R 4.3.3)
#>  reprex        2.0.2   2022-08-17 [1] CRAN (R 4.3.1)
#>  rlang         1.1.3   2024-01-10 [2] CRAN (R 4.3.3)
#>  rmarkdown     2.25    2023-09-18 [1] CRAN (R 4.3.1)
#>  rstudioapi    0.15.0  2023-07-07 [1] CRAN (R 4.3.1)
#>  s2            1.1.6   2023-12-19 [1] CRAN (R 4.3.3)
#>  scales        1.2.1   2022-08-20 [1] CRAN (R 4.3.1)
#>  sessioninfo   1.2.2   2021-12-06 [1] CRAN (R 4.3.1)
#>  sf          * 1.0-17  2024-04-10 [1] Github (r-spatial/sf@cfc321a)
#>  stars       * 0.6-6   2024-04-10 [1] Github (r-spatial/stars@d9e84dd)
#>  styler        1.10.2  2023-08-29 [1] CRAN (R 4.3.2)
#>  tibble        3.2.1   2023-03-20 [1] CRAN (R 4.3.1)
#>  tidyselect    1.2.1   2024-03-11 [1] CRAN (R 4.3.3)
#>  units         0.8-5   2023-11-28 [2] CRAN (R 4.3.3)
#>  utf8          1.2.4   2023-10-22 [1] CRAN (R 4.3.3)
#>  vctrs         0.6.5   2023-12-01 [2] CRAN (R 4.3.3)
#>  withr         3.0.0   2024-01-16 [1] CRAN (R 4.3.3)
#>  wk            0.9.1   2023-11-29 [1] CRAN (R 4.3.2)
#>  xfun          0.40    2023-08-09 [1] CRAN (R 4.3.1)
#>  xml2          1.3.5   2023-07-06 [1] CRAN (R 4.3.1)
#>  yaml          2.3.7   2023-01-23 [1] CRAN (R 4.3.0)
#> 
#>  [1] C:/Users/500596972/AppData/Local/R/win-library/4.3
#>  [2] C:/Program Files/R/R-4.3.2/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────
@edzer
Copy link
Member

edzer commented Apr 17, 2024

fall-out from d9e84dd

@edzer edzer closed this as completed in f0156c9 Apr 17, 2024
@alexyshr
Copy link
Author

Thank you. It is working!

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