Stocks

library(readr)
library(pander)
library(tidyr)
library(dplyr)

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
library(kableExtra)

Attaching package: 'kableExtra'
The following object is masked from 'package:dplyr':

    group_rows
stocks <- read_rds("https://github.com/byuistats/data/raw/master/Dart_Expert_Dow_6month_anova/Dart_Expert_Dow_6month_anova.RDS")
library(tidyr)
library(dplyr)

stocks1 <- stocks %>%
  separate(contest_period, into = c("start_period", "end_period"), sep = "-")

stocks1$month_end <- sub("[0-9]+$", "", stocks1$end_period) 
stocks1$year_end <- sub("^[^0-9]+", "", stocks1$end_period)

stocks1$month_end <- tools::toTitleCase(stocks1$month_end)
saveRDS(stocks1, file = "tidy_stocks.rds")
stocks_summarized <- stocks1 %>%
  group_by(month_end, year_end) %>%
  summarize(value = mean(value, na.rm = TRUE), .groups = "drop")

valid_months <- c("January", "February", "March", "April", "May", "June",
                  "July", "August", "September", "October", "November", "December")

stocks_summarized <- stocks_summarized %>%
  filter(month_end %in% valid_months)

stocks_wide <- stocks_summarized %>%
  pivot_wider(
    names_from = year_end,  
    values_from = value     
  ) %>%
  arrange(factor(month_end, levels = month.name)) %>%
  mutate(across(where(is.numeric), ~ ifelse(is.na(.), "-", as.character(round(. / 2, 1))))) %>%
  rename(Month = month_end) %>%
  select(Month, `1990`, everything())

# Displaying the formatted table with kableExtra
stocks_wide %>%
  kable(format = "html", caption = "Monthly Values by Year") %>%
  kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive"),
                full_width = FALSE, 
                position = "left") %>%
  row_spec(0, bold = TRUE, color = "white", background = "#4e79a7")  # Custom header styling
Monthly Values by Year
Month 1990 1991 1992 1993 1994 1995 1996 1997 1998
January - -9.7 5.3 0.1 9.6 4.2 6.8 5.3 0.8
February - 7.9 11.9 -1.4 1.8 - 9.3 15.4 3
March - 7.9 5.6 8.8 -0.5 -0.6 11.7 -2.9 4.4
April - 23.3 0.7 -2 -0.8 4.4 8.3 -1.3 4.9
May - 16.8 4 6.9 -1.1 6.6 3.8 0.4 6.5
June 2.5 9 -1.6 1 -4.7 16.8 -0.7 9.1 7.1
July 6.6 11.6 1.1 -1.7 0.3 11.1 -0.8 6 6
August -2.4 19.2 0.1 0.3 4.3 8.9 4.4 4.8 -8.7
September -6.1 3.6 -2.3 -1.6 5.9 15.4 3.7 17.7 -11
October -10.4 9.1 -0.7 1 7.4 6.4 2.7 10.3 -
November -12.2 -3.9 -0.2 -4 5 11.3 4.4 1.1 -
December -7 3.8 2.7 - -1.2 2.8 10.6 -1.4 -