library(tidyverse)

Nice Example: Here is a nice example from the tidyr website.

stocks <- data.frame(
  time = as.Date('2009-01-01') + 0:9,
  X = rnorm(10, 0, 1),
  Y = rnorm(10, 0, 2),
  Z = rnorm(10, 0, 4)
)
stocks
stocksm <- stocks %>% gather(stock, price, -time)
stocksm
stocksm %>% spread(stock, price)
#>          time           X           Y          Z
#> 1  2009-01-01 -0.25148344  0.03835518 -0.9835856
#> 2  2009-01-02  0.44479712  0.05912151 -4.7102532
#> 3  2009-01-03  2.75541758  1.09965508 -3.9034025
#> 4  2009-01-04  0.04653138 -4.54822971  4.2602293
#> 5  2009-01-05  0.57770907  5.36511437  0.5266825
#> 6  2009-01-06  0.11819487 -0.72244251  1.9545152
#> 7  2009-01-07 -1.91172049  0.42671150 -6.7978023
#> 8  2009-01-08  0.86208648  2.14869176 -5.8829452
#> 9  2009-01-09 -0.24323674 -1.33017650  1.1366014
#> 10 2009-01-10 -0.20608719  2.22790484  5.3492817
stocksm %>% spread(time, price)
#>   stock  2009-01-01  2009-01-02 2009-01-03  2009-01-04 2009-01-05 2009-01-06
#> 1     X -0.25148344  0.44479712   2.755418  0.04653138  0.5777091  0.1181949
#> 2     Y  0.03835518  0.05912151   1.099655 -4.54822971  5.3651144 -0.7224425
#> 3     Z -0.98358565 -4.71025324  -3.903402  4.26022928  0.5266825  1.9545152
#>   2009-01-07 2009-01-08 2009-01-09 2009-01-10
#> 1 -1.9117205  0.8620865 -0.2432367 -0.2060872
#> 2  0.4267115  2.1486918 -1.3301765  2.2279048
#> 3 -6.7978023 -5.8829452  1.1366014  5.3492817

Download some stock market data. Nice and tidy.

Using quantmod

library(quantmod)
getSymbols("GOOG",src="yahoo") # from yahoo finance 
[1] "GOOG"
getSymbols("GOOG",src="yahoo") 
[1] "GOOG"
barChart(GOOG) 

GOOG %>% head()
           GOOG.Open GOOG.High GOOG.Low GOOG.Close GOOG.Volume GOOG.Adjusted
2007-01-03  231.4944  236.7899 229.0652   232.2842    15513200      232.2842
2007-01-04  232.9847  240.4114 232.6618   240.0686    15877700      240.0686
2007-01-05  239.6910  242.1749 237.5102   242.0209    13833500      242.0209
2007-01-08  242.2693  243.3522 239.5420   240.2276     9570600      240.2276
2007-01-09  241.1565  242.5475 239.0452   241.1814    10832700      241.1814
2007-01-10  240.6498  245.1803 239.4625   243.1486    12014600      243.1486

We will use the BatchGetSymbols R package to download stock market data.

library(BatchGetSymbols)
library(tidyverse)
# set dates
first.date <- Sys.Date() - 10
last.date <- Sys.Date()
# set tickers
tickers <- c('F','Goog','AAPL')
l.out <- BatchGetSymbols(tickers = tickers, 
                         first.date = first.date,
                         last.date = last.date, 
                         cache.folder = file.path(tempdir(), 
                                                  'BGS_Cache') ) # cache in tempdir()

Running BatchGetSymbols for:
   tickers = F, Goog, AAPL
   Downloading data for benchmark ticker | Found cache file
F | yahoo (1|3) | Found cache file - OK!
Goog | yahoo (2|3) | Found cache file - Feels good!
AAPL | yahoo (3|3) | Found cache file - Well done!
print(l.out$df.control)

Is the data in a tidy format?

print(l.out$df.tickers)
l.out$df.tickers %>% ggplot(aes(x = ref.date, y = price.close)) +
   geom_line() +
   facet_wrap(~ticker, scales = 'free_y') 

LS0tCnRpdGxlOiAiU3ByZWFkaW5nIGFuZCBHYXRoZXJpbmciCm91dHB1dDoKICB3b3JkX2RvY3VtZW50OiBkZWZhdWx0CiAgcGRmX2RvY3VtZW50OiBkZWZhdWx0CiAgaHRtbF9ub3RlYm9vazogZGVmYXVsdAotLS0KCmBgYHtyIG1lc3NhZ2U9RkFMU0V9CmxpYnJhcnkodGlkeXZlcnNlKQpgYGAKCk5pY2UgRXhhbXBsZTogIEhlcmUgaXMgYSBuaWNlIGV4YW1wbGUgZnJvbSB0aGUgW3RpZHlyXShodHRwczovL3RpZHlyLnRpZHl2ZXJzZS5vcmcvcmVmZXJlbmNlL3NwcmVhZC5odG1sKSB3ZWJzaXRlLgoKYGBge3J9CnN0b2NrcyA8LSBkYXRhLmZyYW1lKAogIHRpbWUgPSBhcy5EYXRl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