Using Quandl from R
Quandl's free R app allows you to access Quandl data directly from within R.
If you haven't already read Quandl's API documentation, we recommend you do so before proceeding.
Quandl is available within the R console as a free package on CRAN. To download and install from CRAN, simply type:
Sometimes a more recent version of the Quandl package may be available on Github. (CRAN packages often update with a lag). To download and install the latest version from Github, enter this code snippet:
install.packages("devtools") library(devtools) install_github('R-package','quandl') library(Quandl)
To make more than 50 calls a day, you will need an authentication token. You can find your auth_token under the API tab on your account page.
Once you have your auth_token, then after the Quandl library is loaded type this (including the quotes):
Now when you call the Quandl function in R, it will automatically use your auth_token to connect with the Quandl API.
Just pick a dataset and enter its Quandl code, like this:
mydata = Quandl("FRED/GDP")
The Quandl package is able to return data in 4 very usable formats: data frame ("raw"), ts ("ts"), zoo ("zoo") and xts ("xts"). The default is "raw". Here's how to get your data in ts format:
mytimeseries = Quandl("NSE/OIL", type="ts")
You can get multiple datasets in one call by passing an array of Quandl codes, like this:
mydata = Quandl(c("NSE/OIL.4","WIKI/AAPL.1"))
This grabs the 4th column of dataset NSE/OIL and the 1st column of dataset WIKI/AAPL, and returns them in a single call.
Just like the basic API, the R package offers various ways to manipulate or transform the data prior to download:
Specific Date Range:
mydata = Quandl("NSE/OIL", start_date="yyyy-mm-dd", end_date="yyyy-mm-dd")
mydata = Quandl("NSE/OIL", collapse="annual")
mydata = Quandl("NSE/OIL", transformation="rdiff")
(These transformations are documented more fully on our API page.)
For further transformations, you might find our Cheat Sheet on time series analysis using R and Quandl useful.
You can search Quandl for datasets from within the R console. Here is an example search:
Quandl.search(query = "crude oil", page = 2, source = "DOE", silent = TRUE)
The only mandatory parameter is "query". "page" specifies which page of search results you wish to see (default=1). "source" specifies that you want to search within a particular data source (default=all data sources). "silent" specifies whether or not you want to see the first 3 results on the console.
You can upload a dataset to Quandl directly from the R console. Here is an example of dataset creation:
Quandl.push("MYDATASET", name = "I made a dataset", data=Quandl("WIKI/AAPL", rows=5))
To create a dataset you require a table code, a dataset name and some data. The data should be formatted as a data frame with dates in the first column. Once your dataset is created you may modify it and pass as many or as few parameters as you like:
Quandl.push("MYDATASET", description = "This dataset was updated by me", update=TRUE)
Update defaults to FALSE. If update is set to FALSE, you will be explicitly asked if you would like to update your dataset.
This function requires an authentication token, so as to associate created datasets with your account.
For a more powerful way to upload data to Quandl, check out the Quandl toolbelt.
We have created a simple cheat sheet for R users; it covers basic R syntax, vector and matrix operations, time series manipulation, plotting, and Quandl data access. You can download the cheat sheet here.
I am not an R expert. Please do email me with suggestions, feedback, bug reports, feature requests or code contributions. Thank you for your support!
Our thanks to Gergely Daróczi, Paul Gilbert and Hideyoshi Maeda for their help with this package.