# install.packages("data.table")
library(data.table)

# install.packages("ggplot2")
library(ggplot2)

Date Values in R

Dates are represented as the number of days since 1970-01-01, with negative values for earlier dates.

date1 = "2008-09-15"

date1 = as.Date(date1)
class(date1)
## [1] "Date"
date2 = as.Date("2010-06-23")

Difference between dates

date2-date1
## Time difference of 646 days

Current date

cdate = Sys.Date()

cdate - date1
## Time difference of 4175 days

Date formats

  • %d - day as a number (0-31)
  • %a - abbreviated weekday (Mon)
  • %A - unabbreviated weekday (Monday)
  • %m - month number (1-12)
  • %b - abbreviated month (Jan)
  • %B - unabbreviated month (January)
  • %y - 2-digit year (20)
  • %Y - 4-digit year (2020)

Convert today to unabbreviated month day, year format

date3 = "03/21/2011"

date3 = as.Date(date3,format = "%m/%d/%Y")


date4 = "Jan 03 2020"

date4 = as.Date(date4,format="%b %d %Y")

Convert today to mm/dd/yyyy format

format(cdate,format="%m/%d/%Y")
## [1] "02/20/2020"
format(cdate,format="%B %d, %Y")
## [1] "February 20, 2020"

Read Tesla, Inc. Stock Prices from the Following URL

https://raw.githubusercontent.com/dimuthu999/4820/master/TSLA.csv

Tesla = fread("https://raw.githubusercontent.com/dimuthu999/4820/master/TSLA.csv")

tail(Tesla)
##          Date TSLA.Open TSLA.High TSLA.Low TSLA.Close TSLA.Volume TSLA.Adjusted
## 1: 2020-02-10    800.00    819.99   752.40     771.28    24689200        771.28
## 2: 2020-02-11    768.79    783.51   758.00     774.38    11697500        774.38
## 3: 2020-02-12    777.87    789.75   763.37     767.29    12022500        767.29
## 4: 2020-02-13    741.84    818.00   735.00     804.00    26289300        804.00
## 5: 2020-02-14    787.22    812.97   785.50     800.03    15693700        800.03
## 6: 2020-02-18    841.60    860.00   832.36     858.40    16381700        858.40

Convert ‘Date’ column to Date type

Tesla[,Date := as.Date(Tesla$Date)]

tail(Tesla,10)
##           Date TSLA.Open TSLA.High TSLA.Low TSLA.Close TSLA.Volume
##  1: 2020-02-04    882.96    968.99   833.88     887.06    60938800
##  2: 2020-02-05    823.26    845.98   704.11     734.70    48423800
##  3: 2020-02-06    699.92    795.83   687.00     748.96    39880800
##  4: 2020-02-07    730.55    769.75   730.00     748.07    17063500
##  5: 2020-02-10    800.00    819.99   752.40     771.28    24689200
##  6: 2020-02-11    768.79    783.51   758.00     774.38    11697500
##  7: 2020-02-12    777.87    789.75   763.37     767.29    12022500
##  8: 2020-02-13    741.84    818.00   735.00     804.00    26289300
##  9: 2020-02-14    787.22    812.97   785.50     800.03    15693700
## 10: 2020-02-18    841.60    860.00   832.36     858.40    16381700
##     TSLA.Adjusted
##  1:        887.06
##  2:        734.70
##  3:        748.96
##  4:        748.07
##  5:        771.28
##  6:        774.38
##  7:        767.29
##  8:        804.00
##  9:        800.03
## 10:        858.40

Plot share price

ggplot(Tesla,aes(x=Date,y=TSLA.Close))+geom_line()

Create a new column ‘Year’ with the year

Tesla[,Year:=year(Tesla$Date)]

head(Tesla)
##          Date TSLA.Open TSLA.High TSLA.Low TSLA.Close TSLA.Volume TSLA.Adjusted
## 1: 2010-06-29     19.00     25.00    17.54      23.89    18766300         23.89
## 2: 2010-06-30     25.79     30.42    23.30      23.83    17187100         23.83
## 3: 2010-07-01     25.00     25.92    20.27      21.96     8218800         21.96
## 4: 2010-07-02     23.00     23.10    18.71      19.20     5139800         19.20
## 5: 2010-07-06     20.00     20.00    15.83      16.11     6866900         16.11
## 6: 2010-07-07     16.40     16.63    14.98      15.80     6921700         15.80
##    Year
## 1: 2010
## 2: 2010
## 3: 2010
## 4: 2010
## 5: 2010
## 6: 2010

Calculate annual average stock price and annual average volume

summarydata = Tesla[,.(meanprice=mean(TSLA.Close),meanvol=mean(TSLA.Volume)),by=Year]

head(summarydata,20)
##     Year meanprice  meanvol
##  1: 2010  23.34185  1579762
##  2: 2011  26.80476  1290615
##  3: 2012  31.16860  1229796
##  4: 2013 104.40123  8407722
##  5: 2014 223.32909  6913973
##  6: 2015 230.04290  4318325
##  7: 2016 209.76726  4612546
##  8: 2017 314.31630  6334786
##  9: 2018 317.30992  8612107
## 10: 2019 273.53020  9159040
## 11: 2020 618.00032 22735400

Create a line graph showing annual average stock price

ggplot(summarydata,aes(x=Year,y=meanprice))+geom_line()

Read S&P500 Data

SP500Index = fread("https://raw.githubusercontent.com/dimuthu999/4820/master/SP500.csv")

SP500Index[,Date:=as.Date(SP500Index$Date)]
head(SP500Index)
##          Date   SP500
## 1: 2007-01-03 1416.60
## 2: 2007-01-04 1418.34
## 3: 2007-01-05 1409.71
## 4: 2007-01-08 1412.84
## 5: 2007-01-09 1412.11
## 6: 2007-01-10 1414.85

Merge Tesla and SP500Index

Tesla = merge(Tesla,SP500Index,by="Date")

head(Tesla)
##          Date TSLA.Open TSLA.High TSLA.Low TSLA.Close TSLA.Volume TSLA.Adjusted
## 1: 2010-06-29     19.00     25.00    17.54      23.89    18766300         23.89
## 2: 2010-06-30     25.79     30.42    23.30      23.83    17187100         23.83
## 3: 2010-07-01     25.00     25.92    20.27      21.96     8218800         21.96
## 4: 2010-07-02     23.00     23.10    18.71      19.20     5139800         19.20
## 5: 2010-07-06     20.00     20.00    15.83      16.11     6866900         16.11
## 6: 2010-07-07     16.40     16.63    14.98      15.80     6921700         15.80
##    Year   SP500
## 1: 2010 1041.24
## 2: 2010 1030.71
## 3: 2010 1027.37
## 4: 2010 1022.58
## 5: 2010 1028.06
## 6: 2010 1060.27

Plot Tesla and SP500

ggplot(Tesla)+geom_line(aes(x=Date,y=TSLA.Close))+geom_line(aes(x=Date,y=SP500/10),color="red")