Data Science Date & Time Analysis With R

Date & Time Analysis With R

-

- Advertisment -

From this article, a beginner to an advanced R user will be able to reproduce relevant date-related columns from given dates and learn how they are applied in a time series visualisation.

Prerequisites

  1. Linux Arch system
  2. Installed R
  3. Date data

Setting up the environment

The core packaged used in the above analysis are the tidyverse, lubridate, dplyr & date:

Install the packages with:

After installing the packages, load the packages in the R environment:

Set the working directory where your date data has been stored:

Now import the date data:

The attach() function attaches the data to the R search path. It is important to note the type of file will determine the import file code. For this case, the date file in a .csv file. For an excel file use:

Below is the view of the data in the .csv file.

Date & Time columns in date.csv

The date.csv file has two variables; Date and Time.
For simple and direct analysis of dates, ensure they are saved in the format “2021-02-23” such that:
“2021” is the year(yyyy)
“02” is the month(mm)
“23” the day(dd)

From the Dates column, the Year, Month, Week, Day columns are to be produced. We also would like to format the date into the “mmddyyyy” format (i.e. without the “-” hyphens). The Time variable is recorded in 12hr format.

Analysis

Dates

Set the Date variable as “Date” datatype;

The class() function returns dataype of a variable.

From the Date column produce the year, month and day(numeric) variable and store them in a dataframe df_date by running:

View output:


year,month and day columns produced from the Date variables

As noted from the above from the Date, the above code produces the year, month & day columns.
Now, to format days and months from numeric values to abbreviations as well as produces the weeks of the year and the formated “mmddyyyy” column run:

Let’s break down the above:

  • df_date <- df_date…: Assigns/mounts the final output of the code on the previous output.
  • add_column(): a tidyverse function that enables one to add a new column(s) to the data frame date and assign values to the columns. One is also able to position the new column.
  • Column names and values:
    a. D_time = paste(df_date$month,df_date$day,df_date$year, sep = "")
    The variable name for this column is “D_time” and the assigned values are formed by merging the month, day & years columns from the date dataframe.
    The output of this column is: 2232021 where (2,23,2021: month, day, year)

    b. Year = df_date$year
    Pastes the values of the year variables in date output to the new variable “Year”

    c. Month = month.abb[df_date$month]
    month.abb[date$month]: The date$month refers to the numeric monthly assignments of the month column in the date output. The month.abb[] transforms these numerical months into abbreviations. (Jan, Feb, Mar, …)

    d. Week = paste(lubridate::isoweek(df_date$Date), sep = "")
    The isoweek() function from the lubridate package returns the numeric week of the year of a given date according to ISO 8601.

    e. Day = weekdays(df_date$Date, abbreviate = TRUE)
    The weekdays() function returns the day of the week. The abbreviate = TRUE functions to return the abbreviated day of the week. (Mon, Tue, …)

    f. .after = "Date"
    The function places the added columns after the Date column in date output.

Now drop the numeric month and day variables and the duplicated “year”:



The final output for the date data will be:

Run View(df_date)

Final date output

Time

To change the time format from a 12hrs to a 24hrs system, do:

To have a MySQL and a Kibana kind of timestamp, merge the D_time and the 24hrs converted time.

One should have the below output as the final output of the transformed date and time date:

Final df_date output

Write the output to a .csv file in your local repository:

Application of Dates & Time

Dates and time are well known for showing real-time periodical trends i.e. events monitored in units of time. In real-time applications, dates are used in the visualisation of stock trends from yahoo finance. Here, we will visualize Apple Inc (AAPL), Tesla (TSLA) & Nike (NKE) stock trends in the markets using different time units.

To start off, import the relevant database and visualisation packages:

The quantmod package is designed to assist the quantitative trader in the development, testing,
and deployment of statistically based trading models. in this package, one can directly extract realtime data from Yahoo Finance. ggplot2 & plotly will help in visualisation while maggrittr and broom will assist in general data manipulation.

Now set the period of data you need for visualisation:

Request quantmod to get the stock prices for Nike, Tesla & Apple. This will automatically generate three xts objects with an index column to reference the dates:

Now create a dataframe that only captures the “adjusted price column of each stock company. This data frame is transformed into a time series object with the function as.xts():

Before plotting the stocks:

  1. Assigning names to the columns given
  2. Define the index column as a date.

Date is now ready for visualisation:

Visualisation

1. Daily Stock Price trends
ggplot() in ggplot2 package will help in visualizing the To visualize this. To view the Apple, Tesla and Nike stock trends run:

Output:

While the dates on the x-axis have been summarised in years the daily values can be visualised by the ggplotly() which is from the plotly package. The ggplotly(stocks_series) produces a live visual for tracing daily trends:

2. For Tesla (TSLA) trends:

Output:

3. Monthly averages for the AAPL and NKE stocks

Unlike the daily trends, the time series below shows the monthly average trend for the AAPL & NKE stocks. You will notice that the stocks data only has the index as dates. On that note, one will first have to extract months from the date and add it as a variable after changing the xts object to a dataframe. Thereafter find monthly averages and plot them. To do this run:

output:

The compiled R code can be found is the git repository.

Refferences

2 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Posts

Building an Exam Enrollment and Grade Notification System using Africa’s Talking SMS Shortcode API in Django

Introduction This article explores the application of SMS shortcodes to create transparency and improve the quality of education in learning...

Build a user account management system using USSD and SMS API in Go

Introduction We will be learning how to use both the Africastalking SMS and USSD api by building an application where...

Date & Time Analysis With R

One will learn how to analyse dates and time; reproducing date-related columns from a date and also format dates and time. In application, one will learn how to plot simple and multiple time series data using the R language.

Two Factor Authentication With PHP and Africa’s Talking SMS API

Two factor authentication is an additional layer of security used to ensure only authenticated users gain access to an online account. because Passwords are historically weak, and can be easily stolen, it can't alone be the way that users access their accounts.
- Advertisement -

Building a masked number system using Spring Boot, Android and Voice Apis from Africa’s Talking

Introduction In this walk through we shall be building a platform to enable a hypothetical company have their agents call...

React Native, Java Spring Boot Outbound Call Center with Africa’s Talking

Introduction Did you know you can easily create a call centre by simply integrating with Africa's Talking? You can actually...
- Advertisement -

You might also likeRELATED
Recommended to you