This R Programming Challenge is more of a data manipulation exercise than a programming exercise. Here we will use a data file of daily stock prices and volume to exercise some basic needed data skills on the R data frame.
Here are the data exercises:
Read in the data file and check the ‘structure’ of the data. Is the variable Date a factor, string, or date? What data type should it be?
# Read data
jpm = read.csv("~/JPM.csv", header = T)
# Check date class
class(jpm$Date)## [1] "factor"
Date is read as a factor variable by R.
Type cast the variable Date into an R date type named TradeDate. Make sure that TradeDate is attached to your data frame.
jpm$TradeDate = as.Date(jpm$Date, format = "%d-%b-%y")Create a seven character Year-Month string named YearMonth. Make sure that YearMonth is attached to your data frame.
jpm$YearMonth = substr(jpm$TradeDate, 1, 7)Compute the total trading volume for each YearMonth.
round(by(jpm$Volume, jpm$YearMonth, sum), digits = 2)## jpm$YearMonth: 2014-01
## [1] 426148286
## --------------------------------------------------------
## jpm$YearMonth: 2014-02
## [1] 347322382
## --------------------------------------------------------
## jpm$YearMonth: 2014-03
## [1] 425954355
## --------------------------------------------------------
## jpm$YearMonth: 2014-04
## [1] 409023463
## --------------------------------------------------------
## jpm$YearMonth: 2014-05
## [1] 315112129
## --------------------------------------------------------
## jpm$YearMonth: 2014-06
## [1] 263715897
## --------------------------------------------------------
## jpm$YearMonth: 2014-07
## [1] 317345809
## --------------------------------------------------------
## jpm$YearMonth: 2014-08
## [1] 241563417
## --------------------------------------------------------
## jpm$YearMonth: 2014-09
## [1] 264948567
## --------------------------------------------------------
## jpm$YearMonth: 2014-10
## [1] 415470600
## --------------------------------------------------------
## jpm$YearMonth: 2014-11
## [1] 211535572
## --------------------------------------------------------
## jpm$YearMonth: 2014-12
## [1] 347657497
## --------------------------------------------------------
## jpm$YearMonth: 2015-01
## [1] 468205871
## --------------------------------------------------------
## jpm$YearMonth: 2015-02
## [1] 319008175
## --------------------------------------------------------
## jpm$YearMonth: 2015-03
## [1] 332554476
## --------------------------------------------------------
## jpm$YearMonth: 2015-04
## [1] 325069427
## --------------------------------------------------------
## jpm$YearMonth: 2015-05
## [1] 267794319
## --------------------------------------------------------
## jpm$YearMonth: 2015-06
## [1] 337237622Compute the average closing price for each YearMonth.
round(by(jpm$Close, jpm$YearMonth, mean), digits = 2)## jpm$YearMonth: 2014-01
## [1] 57.49
## --------------------------------------------------------
## jpm$YearMonth: 2014-02
## [1] 56.93
## --------------------------------------------------------
## jpm$YearMonth: 2014-03
## [1] 58.87
## --------------------------------------------------------
## jpm$YearMonth: 2014-04
## [1] 57.05
## --------------------------------------------------------
## jpm$YearMonth: 2014-05
## [1] 54.49
## --------------------------------------------------------
## jpm$YearMonth: 2014-06
## [1] 57.12
## --------------------------------------------------------
## jpm$YearMonth: 2014-07
## [1] 57.69
## --------------------------------------------------------
## jpm$YearMonth: 2014-08
## [1] 57.5
## --------------------------------------------------------
## jpm$YearMonth: 2014-09
## [1] 60.21
## --------------------------------------------------------
## jpm$YearMonth: 2014-10
## [1] 58.5
## --------------------------------------------------------
## jpm$YearMonth: 2014-11
## [1] 60.69
## --------------------------------------------------------
## jpm$YearMonth: 2014-12
## [1] 61.48
## --------------------------------------------------------
## jpm$YearMonth: 2015-01
## [1] 57.49
## --------------------------------------------------------
## jpm$YearMonth: 2015-02
## [1] 58.94
## --------------------------------------------------------
## jpm$YearMonth: 2015-03
## [1] 61.04
## --------------------------------------------------------
## jpm$YearMonth: 2015-04
## [1] 62.28
## --------------------------------------------------------
## jpm$YearMonth: 2015-05
## [1] 65.61
## --------------------------------------------------------
## jpm$YearMonth: 2015-06
## [1] 67.88Compute the average daily intraday price spread for each YearMonth. Note that this intraday price spread is computed by the daily high minus the daily low.
jpm$IDPS = (jpm$High - jpm$Low)
round(by(jpm$IDPS, jpm$YearMonth, mean), digits = 2)## jpm$YearMonth: 2014-01
## [1] 0.97
## --------------------------------------------------------
## jpm$YearMonth: 2014-02
## [1] 0.89
## --------------------------------------------------------
## jpm$YearMonth: 2014-03
## [1] 0.96
## --------------------------------------------------------
## jpm$YearMonth: 2014-04
## [1] 0.93
## --------------------------------------------------------
## jpm$YearMonth: 2014-05
## [1] 0.65
## --------------------------------------------------------
## jpm$YearMonth: 2014-06
## [1] 0.69
## --------------------------------------------------------
## jpm$YearMonth: 2014-07
## [1] 0.68
## --------------------------------------------------------
## jpm$YearMonth: 2014-08
## [1] 0.7
## --------------------------------------------------------
## jpm$YearMonth: 2014-09
## [1] 0.73
## --------------------------------------------------------
## jpm$YearMonth: 2014-10
## [1] 1.12
## --------------------------------------------------------
## jpm$YearMonth: 2014-11
## [1] 0.68
## --------------------------------------------------------
## jpm$YearMonth: 2014-12
## [1] 1.08
## --------------------------------------------------------
## jpm$YearMonth: 2015-01
## [1] 1.39
## --------------------------------------------------------
## jpm$YearMonth: 2015-02
## [1] 0.89
## --------------------------------------------------------
## jpm$YearMonth: 2015-03
## [1] 0.9
## --------------------------------------------------------
## jpm$YearMonth: 2015-04
## [1] 0.87
## --------------------------------------------------------
## jpm$YearMonth: 2015-05
## [1] 0.82
## --------------------------------------------------------
## jpm$YearMonth: 2015-06
## [1] 0.8Use {base} R graphics to make this plot.
boxplot(jpm$IDPS ~ jpm$YearMonth, las = 2,
main = "Boxplots of Intraday Price Spread")Use {base} R graphics to make this plot.
bp = barplot(by(jpm$Close, jpm$YearMonth, mean), las = 2,
main = "Average Closing Price",
ylim = c(0, 80))
abline(h = mean(jpm$Close), col = "blue", lty = 5)rm(bp)