I want the mean and median of the numeric columns and date columns:


library(dplyr)
library(lubridate)

summarize_data <- function(data) {
  # Step 1: Summarize main data
  summarized_data <- data %>%
    select(Dates, Week, DEC_BASIS, PEO_BASIS, ESL_BASIS, FUT_1, FUT_2, Year, Crop_Progression) %>%
    group_by(Year) %>%
    reframe(
      across(c(DEC_BASIS, PEO_BASIS, ESL_BASIS, FUT_1, FUT_2, Crop_Progression),
             list(
               min = ~ifelse(all(is.na(.)), NA, min(., na.rm = TRUE)),
               max = ~ifelse(all(is.na(.)), NA, max(., na.rm = TRUE)),
               week_min = ~ifelse(all(is.na(.)), NA, Week[which.min(.)]),
               week_max = ~ifelse(all(is.na(.)), NA, Week[which.max(.)])
             ),
             .names = "{.col}_{.fn}"),
      
      DEC_BASIS_date_min = as.Date(ifelse(all(is.na(DEC_BASIS)), NA, Dates[which.min(DEC_BASIS)])),
      DEC_BASIS_date_max = as.Date(ifelse(all(is.na(DEC_BASIS)), NA, Dates[which.max(DEC_BASIS)])),
      PEO_BASIS_date_min = as.Date(ifelse(all(is.na(PEO_BASIS)), NA, Dates[which.min(PEO_BASIS)])),
      PEO_BASIS_date_max = as.Date(ifelse(all(is.na(PEO_BASIS)), NA, Dates[which.max(PEO_BASIS)])),
      ESL_BASIS_date_min = as.Date(ifelse(all(is.na(ESL_BASIS)), NA, Dates[which.min(ESL_BASIS)])),
      ESL_BASIS_date_max = as.Date(ifelse(all(is.na(ESL_BASIS)), NA, Dates[which.max(ESL_BASIS)])),
      FUT_1_date_min = as.Date(ifelse(all(is.na(FUT_1)), NA, Dates[which.min(FUT_1)])),
      FUT_1_date_max = as.Date(ifelse(all(is.na(FUT_1)), NA, Dates[which.max(FUT_1)])),
      FUT_2_date_min = as.Date(ifelse(all(is.na(FUT_2)), NA, Dates[which.min(FUT_2)])),
      FUT_2_date_max = as.Date(ifelse(all(is.na(FUT_2)), NA, Dates[which.max(FUT_2)])),
      Crop_Progression_date_min = as.Date(ifelse(all(is.na(Crop_Progression)), NA, Dates[which.min(Crop_Progression)])),
      Crop_Progression_date_max = as.Date(ifelse(all(is.na(Crop_Progression)), NA, Dates[which.max(Crop_Progression)]))
    ) %>%
    select(
      Year,
      DEC_BASIS_min, DEC_BASIS_week_min, DEC_BASIS_date_min, DEC_BASIS_max, DEC_BASIS_week_max, DEC_BASIS_date_max,
      PEO_BASIS_min, PEO_BASIS_week_min, PEO_BASIS_date_min, PEO_BASIS_max, PEO_BASIS_week_max, PEO_BASIS_date_max,
      ESL_BASIS_min, ESL_BASIS_week_min, ESL_BASIS_date_min, ESL_BASIS_max, ESL_BASIS_week_max, ESL_BASIS_date_max,
      FUT_1_min, FUT_1_week_min, FUT_1_date_min, FUT_1_max, FUT_1_week_max, FUT_1_date_max,
      FUT_2_min, FUT_2_week_min, FUT_2_date_min, FUT_2_max, FUT_2_week_max, FUT_2_date_max,
      Crop_Progression_min, Crop_Progression_week_min, Crop_Progression_date_min, Crop_Progression_max, Crop_Progression_week_max, Crop_Progression_date_max
    ) %>%
    mutate(Year = as.character(Year))  # Convert Year to character
  
  # Step 2: Create mean row for numeric columns
  mean_row <- summarized_data %>%
    summarize(across(where(is.numeric), ~ mean(., na.rm = TRUE)),
              across(where(is.Date), ~ mean(month(.), na.rm = TRUE))) %>%
    mutate(Year = "Mean")  # Set Year as "Mean" for identification
  
  # Step 3: Create median row for numeric columns
  median_row <- summarized_data %>%
    summarize(across(where(is.numeric), ~ median(., na.rm = TRUE)),
              across(where(is.Date), ~ median(month(.), na.rm = TRUE))) %>%
    mutate(Year = "Median")  # Set Year as "Median" for identification
  
  rbind(summarized_data, mean_row, median_row)
  # Step 4: Bind the mean and median rows to the main summarized data
}