# Libraries
library(timetk)
library(dplyr)
# First value in each month
m4_daily %>%
group_by(id) %>%
summarise_by_time(
.date_var = date,
.by = "month", # Setup for monthly aggregation
# Summarization
value = first(value)
)
# Last value in each month (day is first day of next month with ceiling option)
m4_daily %>%
group_by(id) %>%
summarise_by_time(
.by = "month",
value = last(value),
.type = "ceiling"
) %>%
# Shift to the last day of the month
mutate(date = date %-time% "1 day")
# Total each year (.by is set to "year" now)
m4_daily %>%
group_by(id) %>%
summarise_by_time(
.by = "year",
value = sum(value)
)
Run the code above in your browser using DataLab