I am trying to write a generalize aggregation function where the user specifies the aggregation level or they can aggregate the data over all study dates. The floor_date only converts the first date. why? How can I fix this?
library(dplyr)
library(lubridate)
sTerm <- "year" # month, bimonth, quarter, season, halfyear and year, custom
sCustom <- "2023-2025"
dfDatasetOutput <- data.frame(
valDate=seq(as.Date("2023-01-01"), as.Date("2025-12-01"), by = "month"),
cat1=rnorm(36, 3500, 1000),
cat2=rnorm(36, 2.5, 5)
)
dfDatasetOutput %>%
mutate(valDate=ifelse(toupper(sTerm)=="CUSTOM",
sCustom,
as.character(floor_date(valDate, sTerm))))
# this works just fine
dfDatasetOutput %>%
mutate(valDate=as.character(floor_date(valDate, sTerm)))