You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 namesx<- 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 dplyrthe_condition<- runif(nrow(xy)) <0.2xy<-xy %>%
dplyr::mutate(u= ifelse(the_condition, NA, u))
#convert the dataframe xy to sfpoints<-sf::st_as_sf(xy, coords= c("x", "y"), crs=4326)
#convert points to starspointsstars<-stars::st_as_stars(points)
#create an stars object with 6 dates one per daypointsstars<- c(pointsstars, pointsstars, pointsstars,
pointsstars, pointsstars, pointsstars)
#convert attributes to dimensionspointsstars<- merge(pointsstars)
#change the name of the dimensionspointsstars<- st_set_dimensions(pointsstars, names= c("geometry", "time"))
#create a date sequence with 6 datesthe_dates<- seq.Date(Sys.Date(), by=1, length.out=6)
#set time dimension valuespointsstars<-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 polygonpointsstars1<-pointsstars[ , 1, ]
#remove unused dimensionspointsstars1<-pointsstars1 %>% adroppointsstars1[[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 dimensionspointstarsagg<-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 polygonpointstarsagg1<-pointstarsagg[ , 1, ]
pointstarsagg1[[1]]
#> [,1] [,2] [,3]#> [1,] 0 1 NaN
plot(pointstarsagg)
Before using
aggregate.stars
the values in the originalstars
objectpointsstars1
for 6 days are0, 0, 0, 0, 0, 0
. After aggregating by2 days
usingmean
the values should be0, 0, 0
, but they are0, 1, NaN
(stars objectpointstarsagg1
). In the plot it can be seen that themean
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 objectpointsstars
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?Created on 2024-04-15 with reprex v2.0.2
Session info
The text was updated successfully, but these errors were encountered: