0

I know how to reduce the granularity down to every hour by using & (minute == 0) but how would I reduce it if I want to look at every 4 hours? I can't find anywhere.

filter(data, year == 2009 & week == 50 & (minute == 0))

using lubridate

gbougie
  • 23
  • 4
  • Please make this question *reproducible*. This includes sample code you've attempted (including listing non-base R packages, and any errors/warnings received), sample *unambiguous* data (e.g., `data.frame(x=...,y=...)` or the output from `dput(head(x))`), and intended output given that input. Refs: https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. – r2evans Jan 21 '22 at 05:15
  • `filter(data, hour %% 4 == 0)`? – Jon Spring Jan 21 '22 at 05:26

1 Answers1

1
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union
dates <- c("2021-01-01 02:35:00", "2021-01-02 14:17:24", "2022-04-26 17:03:22")
for (i in 1:length(dates)) {
  newDate <- date(as_datetime(dates[i]))
  newHour <- floor(hour(as_datetime(dates[i])) / 4)
  dateModule4 <- as_datetime(paste(newDate, paste0(newHour * 4, ":00:00"), sep = " "))
  print(dateModule4)
}
#> [1] "2021-01-01 UTC"
#> [1] "2021-01-02 12:00:00 UTC"
#> [1] "2022-04-26 16:00:00 UTC"

Generally, it roundes down to floor/4 == 0

Created on 2022-01-21 by the reprex package (v2.0.1)

Grzegorz Sapijaszko
  • 1,913
  • 1
  • 5
  • 12