6) Extract Reporting Periods from a Date.

This Blog entry is from the Loading and Shaping section in Learn R.

There are many functions that exist to extract useful information from dates such as weekdays, months or quarters which make reporting on dates more native. This procedure focuses on three functions:

·         weekdays() which extracts the particular day of the week (e.g. Monday).

·         months() which extracts the month of the year (e.g. June).

·         quarters() which extracts the quarter of the date in the year (e.g. Q3).

All these functions work in the same manner, in that they take just one date argument and return a value.  In this example, the quarter is to be returned for the purpose of reporting.  To return the quarter value:

ReportingQuarters <- quarters(Dates)
using-the-quarters-function-script-in-r.png

Run the line of script to console:

creating-a-vector-with-the-quarters-function-in-r.png

Writing out the vector typing:

ReportingQuarters
the-results-of-the-quarters-function-written-out-to-the-r-console.png

It can be observed that the new vectors details the quarter extracted from the Dates() vector.  The procedure may be used interchangeable between the weekdays() and months() function.