Little useless-useful R functions – Drawing calendar

[This article was first published on R – TomazTsql, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

This time around, since first half of the 2021 is behind us, we are making Calendar in R.

Simple outline of current month in calendar for 2021

This will be achieved with two useless functions.

Function to draw a month (with optional parameter as current date)

DrawCalendarMonth <- function(InDate=NULL) {
  if(is.null(InDate)){
    date <- Sys.Date()
  } else {
    date <- as.Date(InDate)
  }
  t <- format(date, "%m")
  TT <- format(date, "%B")
  year <- as.integer(format(date, "%Y"))
  
# Leap year calculation
  if((year %% 4) == 0) {
    if((year %% 100) == 0) {
      if((year %% 400) == 0) {
        leap <- TRUE
      } else { leap <- FALSE }
    } else { leap <- TRUE }
  } else { leap <- FALSE }
  
#number of days in month
  if (t %in% c("01","03","05", "07", "08", "10", "12")){
    nofDays <- 31
  } 
  if (t %in% c("04","06","09", "11")){
    nofDays <- 30
  }
  if (t %in% c("02") & leap == TRUE){
    nofDays <- 29
  } 
  if (t %in% c("02") & leap == FALSE){
    nofDays <- 28
  }
  
  firstDay <- as.Date(paste0(format(date, "%Y-%m"), "-01"))
  name1D <- weekdays(firstDay)
  num1D <- as.POSIXlt(firstDay)$wday
  n.col <- 7 #number of days; constant
  n.row <- 5 #depends on month, number of days and first day in month; must be calculated
  M_y <- 35  # Default matrix size (product of n.col * n.row)
  
  if (num1D > 5 & nofDays > 29) {
    n.row <- 6 #nrow correction
    M_y <- 42
  } 
  
  if (num1D %in% c(1) & nofDays %in% c(28)) {
    n.row <- 4 #nrow correction
    M_y <- 28
  } 
  
  mat <- matrix(1:M_y, nrow = n.row, ncol = n.col, T) #Matrix structure for month
  for(column in 1:n.col){ mat[, column] <- 00 }   #Reset values to 00
  i <- 1 #running value
  
  for(row in 1:n.row){
    for(column in 1:n.col){
      if(row >= 1 & column >= num1D | row > 1) {
        if(i > nofDays){
          mat[row,column] <- 0
          } else {
        mat[row,column] <- i
        i= i + 1
          }
      }
    }
  }
  dd <- as.data.frame(mat)
  colnames(dd) <- c("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")
  cat(paste0("\n", "Month: ",TT, "\n", "Year: ", year, "\n"))
 
  return(dd)
}

Running this function as:

DrawCalendarMonth() 
DrawCalendarMonth("2021-02-21")

Function takes into consideration
1. calculation of leap years,
2. the number of weeks per month and
3. most obvious – number of days per month.

Function to draw a year (with optional parameter year)

Next step is no brainer. Creating calendar for complete year is just 12-times iteration of DrawCalendarMonth function.

DrawYear <- function(Year=NULL){
  if(is.null(Year)){
    year <- as.integer(format(Sys.Date(), "%Y"))
  } else {
    year <- as.integer(Year)
  }
  dateFrom <- as.Date(paste0(year,"0101"), "%Y%m%d")
  dateTo <- as.Date(paste0(year,"1201"), "%Y%m%d")
  FirstOfMonth <- format(seq(dateFrom,dateTo,by="month"), "%Y-%m-%d")

  for (i in 1:length(FirstOfMonth)){
    r <- DrawCalendarMonth(FirstOfMonth[i])
    print(r)
    }
}

Running it as:

# Draw years' calendar
DrawYear(2020)
DrawYear()

And result is a complete calendar ?

Useless calendar for 2021

As always, code is available in at the Github in same Useless_R_function repository. And this function is here.

Happy R-coding!

What are next step for these two functions:

  • Create export to Markdown (because why not)
  • Sort out the week enumerator (in both functions)
  • Set the start of week (Sunday or Monday)
  • Draw it with ggplot2 library and colour all important dates, bank holidays and festivals

To leave a comment for the author, please follow the link and comment on their blog: R – TomazTsql.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)