Tuesday, September 12, 2017

A Function to Retrieve Time Series by Variable Number from CanSim (Statistics Canada)

In this post, I want to make a function available that allows you to download a time series from Statistics Canada directly into R. The function is a wrapper for the get_cansim_vector function from the 'cansim' package. It determines the frequency of a series and converts it into a time series.

 All CanSim variables start with a "v". To find this vector name, you need to change the view of a table you find on the Statistics Canada website by clicking 'Add/Remove Data' => 'Customize Layout' and then tick the box  'Display Vector identifier and coordinate'

Below is the function that will do the trick. The function works with daily, weekly, monthly, quarterly and yearly data. If the series does not have one of these frequencies, the data is still returned, but not properly converted into a time series. 



Now we can call the function retrieving the variable from CanSim. 



Let me know if you have any comments or suggestions.

- this post was updated 2020-11-16 - 

No comments:

Post a Comment