Read Data. Call the data frames “IBM”, “GE”, “ProcterGamble”, “CocaCola”, and “Boeing”, respectively. Each data frame has two variables.

Convert from factor object a “Date” object

IBM$Date = as.Date(IBM$Date, "%m/%d/%y")
GE$Date = as.Date(GE$Date, "%m/%d/%y")
CocaCola$Date = as.Date(CocaCola$Date, "%m/%d/%y")
ProcterGamble$Date = as.Date(ProcterGamble$Date, "%m/%d/%y")
Boeing$Date = as.Date(Boeing$Date, "%m/%d/%y")

VISUALIZING STOCK DYNAMICS

plot(CocaCola$Date, CocaCola$StockPrice, type="l", col="red")
lines(ProcterGamble$Date, ProcterGamble$StockPrice, col="blue")
abline(v=as.Date(c("2000-03-01")), lwd=2)  ##draw a vertical line at a certain date

### VISUALIZING STOCK DYNAMICS 1995-2005

plot(CocaCola$Date[301:432], CocaCola$StockPrice[301:432], type="l", col="red", ylim=c(0,210))
lines(ProcterGamble$Date[301:432], ProcterGamble$StockPrice[301:432], col="blue")
lines(IBM$Date[301:432], IBM$StockPrice[301:432], col="green")
lines(GE$Date[301:432], GE$StockPrice[301:432], col="purple")
lines(Boeing$Date[301:432], Boeing$StockPrice[301:432], col="orange")
abline(v=as.Date(c("2000-03-01")), lwd=2) #the stock for General Electric falls significantly more than the other stocks after the technology bubble burst.
abline(v=as.Date(c("1997-09-01")), lwd=2) #two companies had a decreasing trend in stock prices from September 1997 to November 1997: Boeing and Procter & Gamble.
abline(v=as.Date(c("1997-11-01")), lwd=2)

Calculate the mean stock price of these 5 companies, which monthy is more than the average?

mean(IBM$StockPrice)
## [1] 144.375
tapply(IBM$StockPrice, months(IBM$Date), mean)
##     April    August  December  February   January      July      June 
##  152.1168  140.1455  140.7593  152.6940  150.2384  139.0670  139.0907 
##     March       May  November   October September 
##  152.4327  151.5022  138.0187  137.3466  139.0885
mean(Boeing$StockPrice)
## [1] 46.59293
tapply(Boeing$StockPrice, months(Boeing$Date),mean)
##     April    August  December  February   January      July      June 
##  47.04686  46.86311  46.17315  46.89223  46.51097  46.55360  47.38525 
##     March       May  November   October September 
##  46.88208  48.13716  45.14990  45.21603  46.30485
mean(GE$StockPrice)
## [1] 59.3035
tapply(GE$StockPrice, months(GE$Date), mean)
##     April    August  December  February   January      July      June 
##  64.48009  56.50315  59.10217  62.52080  62.04511  56.73349  56.46844 
##     March       May  November   October September 
##  63.15055  60.87135  57.28879  56.23897  56.23913
mean(CocaCola$StockPrice)
## [1] 60.02973
tapply(CocaCola$StockPrice, months(CocaCola$Date), mean)
##     April    August  December  February   January      July      June 
##  62.68888  58.88014  59.73223  60.73475  60.36849  58.98346  60.81208 
##     March       May  November   October September 
##  62.07135  61.44358  59.10268  57.93887  57.60024
mean(ProcterGamble$StockPrice)
## [1] 77.70452
tapply(ProcterGamble$StockPrice, months(ProcterGamble$Date), mean)
##     April    August  December  February   January      July      June 
##  77.68671  76.82266  78.29661  79.02575  79.61798  76.64556  77.39275 
##     March       May  November   October September 
##  77.34761  77.85958  78.45610  76.67903  76.62385