2

What I have is this sf-object:

Simple feature collection with 50 features and 8 fields
geometry type:  POLYGON
dimension:      XY
bbox:           xmin: 611398.7 ymin: 5122699 xmax: 745144.8 ymax: 5207956
CRS:            +proj=utm +zone=32 +ellps=WGS84 +units=m +no_defs
First 10 features:
   20171225 20171226 20171227 20171228 20171229 20171230 20171231 20180101                       geometry
1         0      0.0    0.600        0        0        0        0 0.600000 POLYGON ((675867.9 5138308,...
2         0      0.0    0.900        0        0        0        0 1.600000 POLYGON ((671435.1 5122705,...
3         0      0.2    0.000        0        0        0        0 1.600000 POLYGON ((688357.4 5152557,...
4         0      0.7    0.000        0        0        0        0 0.900000 POLYGON ((698123.5 5169814,...
5         0      0.0    0.000        0        0        0        0 0.800000 POLYGON ((672429.8 5197674,...
6         0      0.7    0.000        0        0        0        0 0.900000 POLYGON ((698977.7 5168984,...
7         0      0.0    0.000        0        0        0        0 0.500000 POLYGON ((717004.7 5196926,...
8         0      0.0    0.000        0        0        0        0 3.116667 POLYGON ((622728.9 5152046,...
9         0      0.8    0.025        0        0        0        0 0.900000 POLYGON ((695677.5 5172000,...
10        0      0.9    0.300        0        0        0        0 0.600000 POLYGON ((704247.9 5184909,...

Where each column (minus the geometry-column) is a date, and each row is a polygon. The cells are mean rainfall values for that day and polygon. Now I'd like to calculate the cumulative rainfall for each day and polygon and store that in a clean way in some R-object. Up to now I just worked with spatial objects that either were just one point or one polygon. There I could simply do something like:

# get the geometry column
    geom = df %>% st_geometry()

    df_long = df %>%
      st_drop_geometry() %>%
      pivot_longer(cols = everything(), names_to="dates", values_to="precip") %>%
      mutate(dates = as.Date(dates, "%Y%m%d")) %>%
      # get the cumulative count for the days
      mutate(accumulated = cumsum(precip)) %>%
      mutate(geom = geom) %>%
      st_as_sf()

to get something simple like this:

  dates      precip accumulated
* <date>      <dbl>       <dbl>
1 2017-12-25  0           0    
2 2017-12-26  0           0    
3 2017-12-27  0.600       0.600
4 2017-12-28  0           0.600
5 2017-12-29  0           0.600
6 2017-12-30  0           0.600
7 2017-12-31  0           0.600
8 2018-01-01  0.600       1.20 

However, when I have a spatial object of multiple points or polygons, things get quite messy in my head. I could maybe split the sf-object into nrow(df)-sf-objects and apply the same operation and store them in a list. But I don't feel like this is the best way to go. Maybe someone has some kind of idea on how to approach this:)

0

Your Answer

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

Browse other questions tagged or ask your own question.