5

I have a raster stack (my_stack) and I converted it into a stars object:

mystar <- st_as_stars(my_stack)

That results in:

stars object with 3 dimensions and 1 attribute
attribute(s), summary of first 1e+05 cells:
    layer.1        
 Min.   :  0.0000  
 1st Qu.:  0.0000  
 Median :  0.0000  
 Mean   :  0.7897  
 3rd Qu.:  0.0000  
 Max.   :686.6100  
dimension(s):
     from  to  offset delta                       refsys point               values    
x       1 904  104010  1000 +proj=tmerc +lat_0=0 +lon...    NA                 NULL [x]
y       1 773 4623806 -1000 +proj=tmerc +lat_0=0 +lon...    NA                 NULL [y]
band    1  67      NA    NA                           NA    NA layer.1,...,layer.67    

I also have a vector with Date objects. How could I add this vector as time dimension in mystar object?

1 Answer 1

3

This should work:

mystar = st_set_dimensions(mystar, 3, values = dates, names = "date")

where dates is the Date vector.

4
  • This will replace 3nd dimension with dates. Will not add new dimension.Right?
    – Leonidas
    Commented Oct 21, 2019 at 14:35
  • 1
    Right. The st_set_dimensions function is used to modify the properties of existing dimensions. Commented Oct 22, 2019 at 6:20
  • @MichaelDorman what if the {stars} object doesn't contain a band yet? Commented Jan 28, 2021 at 13:21
  • 1
    @CharlieJoeyHadley If there is no dimension 3 (bands) then this expression will not work. Can't set dimension properties on a non-existing dimension. Commented Jan 28, 2021 at 21:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.