#' "Calendaryify" Data
#'
#' This function calculates daily mean values for every numeric column in a data frame,
#' and adds useful calendar data such as the day of the week, the month, etc. A useful
#' purpose of this could be plotting calendar plots using ggplot().
#'
#' @param df A data frame.
#' @param date_col A character string indicating the date column. Defaults to "date".
#' @return A data frame of daily mean
#' @export
calendarify = function(df, date_col = "date") {
`%>%` <- magrittr::`%>%`
cal_df = df %>%
dplyr::mutate(
day = lubridate::day(.data[[date_col]]),
wday = lubridate::wday(.data[[date_col]], label = T),
week = lubridate::week(.data[[date_col]]),
month = lubridate::month(.data[[date_col]], label = T),
year = lubridate::year(.data[[date_col]])
) %>%
dplyr::mutate(wday = factor(wday, c("Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri"))) %>%
dplyr::group_by(year, month, week, wday, day) %>%
dplyr::summarise(dplyr::across(where(is.numeric), mean, na.rm = T)) %>%
dplyr::group_by(year, month) %>%
dplyr::mutate(week = week - min(week))
return(cal_df)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.