Search code examples
rdataframedplyrlubridate

Creating Calendar df in R


I am currently creating a Calendar df to join to my other dfs and originally code it in the following way:

Date <- seq(as.Date("2020-01-01"), as.Date("2021-12-31"), by="days")
Calendar <- data.frame(Date)

Calendar$DateNo <- format(Calendar$Date, format = "%d")
Calendar$NameDay <- format(Calendar$Date, format = "%A")
Calendar$MonthNo <- format(Calendar$Date, format = "%m")
Calendar$NameMonth <- format(Calendar$Date, format = "%B")
Calendar$NameMonthShort <- format(Calendar$Date, format = "%b")
Calendar$Week <- format(Calendar$Date, format = "%V")
Calendar$Year <- format(Calendar$Date, format = "%Y")
Calendar$Quarter <- quarter(Calendar$Date, with_year = F, fiscal_start = 7)
Calendar$Month_Year <-paste(Calendar$NameMonthShort,Calendar$Year,sep="-")
Calendar$Quarter_Year <-paste(Calendar$Quarter,Calendar$Year,sep="-")

After some issues with plotting my data into ggplot I came across an alternate way of creating it using lubridate package with mutate. My new code is as follows:

Date <- seq(as.Date("2020-01-01"), as.Date("2021-12-31"), by="days")
Calendar <- data.frame(Date)

Calendar <- Calendar %>%
  mutate(
    DateNo = day(Date),
    NameDay = wday(Date,label = TRUE),
    MonthNo = month(Date),
    NameMonth = month(Date, label = TRUE),
    NameMonthShort = month(Date, label = TRUE),
    Week = week(Date),
    Year = year(Date),
    Quarter = quarter(Date, with_year = F, fiscal_start = 7))

The issues I am encountering are that I can't add the unabbreviated date/month and not sure if I can add Month_Year/Quarter_Year inside the mutate so that the values are factored in. Is it possible to add those values in or do I have to add them how I did previously? Thanks!


Solution

  • Long month names are easy to add by including abbr=FALSE switch to month().

    Pasting quarters or months to years needs a second mutate as below.

    Edit Since paste creates character vectors and not factors, you will need to specify factor levels manually:

    monthlevels = c(
                    'Jan-2020','Feb-2020','Mar-2020','Apr-2020','May-2020','Jun-2020',
                    'Jul-2020','Aug-2020','Sep-2020','Oct-2020','Nov-2020','Dec-2020',
                    'Jan-2021','Feb-2021','Mar-2021','Apr-2021','May-2021','Jun-2021',
                    'Jul-2021','Aug-2021','Sep-2021','Oct-2021','Nov-2021','Dec-2021')
                    
    quarterlevels = c('1-2020','2-2020','3-2020','4-2020','1-2021','2-2021','3-2021','4-2021')
    
    
    Calendar %>%
      mutate(
        DateNo = day(Date),
        NameDay = wday(Date,label = TRUE),
        MonthNo = month(Date),
        NameMonth = month(Date, label = TRUE, abbr=FALSE), ## added abbr=FALSE
        NameMonthShort = month(Date, label = TRUE),
        Week = week(Date),
        Year = year(Date),
        Quarter = quarter(Date, with_year = F, fiscal_start = 7)) %>% 
        ## added second mutate() to paste fields created by the first mutate
        mutate(
        QuarterYear = factor(paste(Quarter, Year, sep='-'), levels=quarterlevels),
        MonthYear = factor(paste(NameMonthShort,Year,sep="-"),levels=monthlevels     
        ) %>% head()
    

    Returns:

            Date DateNo NameDay MonthNo NameMonth NameMonthShort Week Year Quarter
    1 2020-01-01      1     Wed       1   January            Jan    1 2020       3
    2 2020-01-02      2     Thu       1   January            Jan    1 2020       3
    3 2020-01-03      3     Fri       1   January            Jan    1 2020       3
    4 2020-01-04      4     Sat       1   January            Jan    1 2020       3
    5 2020-01-05      5     Sun       1   January            Jan    1 2020       3
    6 2020-01-06      6     Mon       1   January            Jan    1 2020       3
      QuarterYear MonthYear
    1      3-2020  Jan-2020
    2      3-2020  Jan-2020
    3      3-2020  Jan-2020
    4      3-2020  Jan-2020
    5      3-2020  Jan-2020
    6      3-2020  Jan-2020