This is an simple tutorial on how to use Quandl. Quandl is like a Google for numerical data that allows you to find and use numerical data easily and fast. It is a open platform that wants to offer everyone a rich collection of high quality numerical data on thousands of subjects.

This simple tutorial will show how to use Quandl within R through a package Quandl.

Importing Quandl Datasets

First, load the library

library(Quandl)
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## 
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric

Try to load a sample data set

mydata <- Quandl("NSE/OIL")
head(mydata)
##         Date   Open   High    Low   Last  Close Total Trade Quantity
## 1 2016-02-01 352.55 359.80 347.75 355.50 356.05               152440
## 2 2016-01-29 351.00 354.90 342.80 349.85 351.55               951671
## 3 2016-01-28 349.00 350.25 342.05 348.20 348.25               690825
## 4 2016-01-27 341.80 347.75 340.45 347.20 347.05               256305
## 5 2016-01-25 349.95 349.95 341.30 345.00 346.05               590308
## 6 2016-01-22 337.00 346.45 332.55 344.40 344.00               314490
##   Turnover (Lacs)
## 1          538.81
## 2         3328.06
## 3         2401.74
## 4          885.80
## 5         2034.52
## 6         1070.41

At Quandl, every dataset is identified by a unique id. This unique id can be found on the Quandl website as the Quandl Code. You can use this ID to access your dataset in R. For example: Get the PragueStockExchange dataset (Quandl code is “PRAGUESE/PX”).

mydata <- PragueStockExchange <-Quandl("PRAGUESE/PX")
head(mydata)
##         Date  Index % Change
## 1 2016-02-01 914.71    -0.69
## 2 2016-01-29 921.07     1.28
## 3 2016-01-28 909.43     0.76
## 4 2016-01-27 902.56     1.09
## 5 2016-01-26 892.81     1.01
## 6 2016-01-25 883.85    -0.33
str(mydata)
## 'data.frame':    5455 obs. of  3 variables:
##  $ Date    : Date, format: "2016-02-01" "2016-01-29" ...
##  $ Index   : num  915 921 909 903 893 ...
##  $ % Change: num  -0.69 1.28 0.76 1.09 1.01 -0.33 3.13 0.46 -2.07 0.71 ...
##  - attr(*, "freq")= chr "daily"

The Quandl package is able to return data in 4 very usable formats:

The only thing you have to do is to give the type you want as an argument to the Quandl() function.

Suppose you want to plot the stock price of Facebook (Use Quandl code: “GOOG/NASDAQ_FB”“), with the help of the quantmod see an shiny example using quantmod package package. This package requires you to use the xts format.

For example, get the Facebook stock quotes to the variable Facebook. Then plot the graph use the quantmod package.

library(quantmod)
## Loading required package: TTR
## Version 0.4-0 included new data defaults. See ?getSymbols.
Facebook <- Quandl("GOOG/NASDAQ_FB", type="xts")
candleChart(Facebook)

Obviously, you do not want to go to the Quandl website every single time you need a dataset in R. Luckily, it is possible to search Quandl datasets using R as follows:

Quandl.search(query = "Search Term", page = int, 
              source = "Specific source to search", 
              silent = TRUE|FALSE)

query is the only mandatory argument and represents the term you wish to search for on Quandl. page is which page of search result you wish to return (default = 1), and source lets you choose the specific source you wish to search for. By putting silent to FALSE you see the first 3 results printed to the console. If silent is set to TRUE it does not print anything to screen.

For example, we want to search dataset for ‘Bitcoin’, and put the results in the results variable

results <- Quandl.search(query="Bitcoin", silent=FALSE)
## EUR/BITCOIN Prices, bitcoin_central
## Code: BAVERAGE/BITCOIN_CENTRALEUR
## Desc: Bitcoin is an experimental new digital currency that enables instant payments to anyone, anywhere in the world. Bitcoin uses peer-to-peer technology to operate with no central authority: managing transactions and issuing money are carried out collectively by the network. Bitcoin is also the name of the open source software which enables the use of this currency. Updated at 6:00pm EST. See more at <a href="http://bitcoincharts.com" rel="nofollow" target="blank">https://bitcoinaverage.com</a>
## Freq: daily
## Cols: Date | Ask | Bid | Last | Volume (BTC) | Volume (% of Total)
## 
## EUR/BITCOIN Prices, bitcoin_de
## Code: BAVERAGE/BITCOIN_DEEUR
## Desc: Bitcoin is an experimental new digital currency that enables instant payments to anyone, anywhere in the world. Bitcoin uses peer-to-peer technology to operate with no central authority: managing transactions and issuing money are carried out collectively by the network. Bitcoin is also the name of the open source software which enables the use of this currency. Updated at 7:00pm EST. See more at <a href="http://bitcoincharts.com" rel="nofollow" target="blank">https://bitcoinaverage.com</a>
## Freq: daily
## Cols: Date | Ask | Bid | Last | Volume (BTC) | Volume (% of Total)
## 
## IDR/BITCOIN Prices, bitcoin_co_id
## Code: BAVERAGE/BITCOIN_CO_IDIDR
## Desc: Bitcoin is an experimental new digital currency that enables instant payments to anyone, anywhere in the world. Bitcoin uses peer-to-peer technology to operate with no central authority: managing transactions and issuing money are carried out collectively by the network. Bitcoin is also the name of the open source software which enables the use of this currency. Updated at 7:00pm EST. See more at <a href="http://bitcoincharts.com" rel="nofollow" target="blank">https://bitcoinaverage.com</a>
## Freq: daily
## Cols: Date | Ask | Bid | Last | Volume (BTC) | Volume (% of Total)
## 
## NextBTC/BITCOIN Price
## Code: CRYPTOCHART/NXBTC
## Desc: Historical price data for NextBTC from exchange with highest number of trades. Volume recalculated to bitcoin. Updated at 6:00pm EST
## Freq: daily
## Cols: Date | Price | Volume
## 
## BlakeBitcoin/BITCOIN Price
## Code: CRYPTOCHART/BBTC
## Desc: Historical price data for BlakeBitcoin from exchange with highest number of trades. Volume recalculated to bitcoin. Updated at 6:00pm EST
## Freq: daily
## Cols: Date | Price | Volume
## 
## Bitcoins/BITCOIN Price
## Code: CRYPTOCHART/BTCXX
## Desc: Historical price data for Bitcoins from exchange with highest number of trades. Volume recalculated to bitcoin. Updated at 6:00pm EST
## Freq: daily
## Cols: Date | Price | Volume
## 
## Bitcoin/BITCOIN Price
## Code: CRYPTOCHART/BTC
## Desc: Historical price data for Bitcoin from exchange with highest number of trades. Volume recalculated to bitcoin. Updated at 6:00pm EST
## Freq: daily
## Cols: Date | Price | Volume
## 
## THESE/BITCOIN Price
## Code: CRYPTOCHART/THESE
## Desc: Historical price data for THESE from exchange with highest number of trades. Volume recalculated to bitcoin. Updated at 6:00pm EST
## Freq: daily
## Cols: Date | Price | Volume
## 
## THEIR/BITCOIN Price
## Code: CRYPTOCHART/THEIR
## Desc: Historical price data for THEIR from exchange with highest number of trades. Volume recalculated to bitcoin. Updated at 6:00pm EST
## Freq: daily
## Cols: Date | Price | Volume
## 
## THERE/BITCOIN Price
## Code: CRYPTOCHART/THERE
## Desc: Historical price data for THERE from exchange with highest number of trades. Volume recalculated to bitcoin. Updated at 6:00pm EST
## Freq: daily
## Cols: Date | Price | Volume

Manipulating Quandl Datasets

The Quandl() function has two arguments, start_date and end_date, that can be used to specify the time range of the data to load. The format to specify the date is: yyyy-mm-dd.

For example, we want to get the exchange rate of USD/EUR from January 1st 2013 up to December 1st 2013, using the ID “BNP/USDEUR”.

Exchange <- Quandl("BNP/USDEUR", start_date="2013-01-01", end_date="2013-12-01")
head(Exchange)
##         Date USD/EUR
## 1 2013-12-01  0.7358
## 2 2013-11-30  0.7358
## 3 2013-11-29  0.7350
## 4 2013-11-28  0.7359
## 5 2013-11-27  0.7364
## 6 2013-11-26  0.7385
tail(Exchange)
##           Date USD/EUR
## 330 2013-01-06  0.7652
## 331 2013-01-05  0.7652
## 332 2013-01-04  0.7672
## 333 2013-01-03  0.7615
## 334 2013-01-02  0.7555
## 335 2013-01-01  0.7578

Quandl can transform your data before serving it. You can set the transform argument to:

Say you want to know the Canadian GDP annual percent change. You will need rdiff transformation. The Quandl ID of it is “FRED/CANRGDPR”.

GDP_change <- Quandl("FRED/CANRGDPR", transform="rdiff")
head(GDP_change)
##         DATE        VALUE
## 1 2011-01-01  0.024055956
## 2 2010-01-01  0.032149365
## 3 2009-01-01 -0.027697687
## 4 2008-01-01  0.006887622
## 5 2007-01-01  0.021999897
## 6 2006-01-01  0.028230247

You don’t always need every available data tick for your analysis. Sometimes having the data available on a daily or weekly base is sufficient. By altering the collapse parameter you can easily indicate the desired frequency. The available options are:

none|daily|weekly|monthly| quarterly|annual

Quandl returns the last observation for the given period. So, if you collapse a daily dataset to “monthly”, you will get a sample of the original dataset where the observation for each month is the last data point available for that month.

For example, Get crude oil prices on a quarterly basis from the EIA’s daily spot price (use the code “DOE/RWTC”)

eiaQuarterly <- Quandl("DOE/RWTC", collapse="quarterly")
head(eiaQuarterly) 
##         Date Value
## 1 2015-12-31 36.36
## 2 2015-09-30 45.06
## 3 2015-06-30 59.48
## 4 2015-03-31 47.72
## 5 2014-12-31 53.45
## 6 2014-09-30 91.17

Two other arguments of the Quandl() function are rows and sort.

The meaning of sort is straightforward. By default it is set on descending (desc), but you can change it to ascending by setting the sort argument to asc.

The rows argument on the other hand allows you to get only the first n rows of your query. For example, you can use rows=1 to get the latest observation of any dataset.

For example, the first 5 observations of the crude oil prices from the EIA (use the code DOE/RWTC) in an ascending order.

TruSo <- Quandl("DOE/RWTC", order="asc", rows=15)
TruSo
##          Date Value
## 1  1986-01-02 25.56
## 2  1986-01-03 26.00
## 3  1986-01-06 26.53
## 4  1986-01-07 25.85
## 5  1986-01-08 25.87
## 6  1986-01-09 26.03
## 7  1986-01-10 25.65
## 8  1986-01-13 25.08
## 9  1986-01-14 24.97
## 10 1986-01-15 25.18
## 11 1986-01-16 23.98
## 12 1986-01-17 23.63
## 13 1986-01-20 21.33
## 14 1986-01-21 20.61
## 15 1986-01-22 20.25

This material is based on DataCamp Quandl Course.