[API] statcanR

API & Databases R Courses

Access Canada’s economy, society and environment data through the statcanR API.

Thierry Warin https://warin.ca/aboutme.html (HEC Montréal and CIRANO (Canada))https://www.hec.ca/en/profs/thierry.warin.html
01-30-2020

Database description

“Statistics Canada is the national statistical office. The agency ensures Canadians have the key information on Canada’s economy, society and environment that they require to function effectively as citizens and decision makers.” (About Us, Statistics Canada)

Statistic Canada : https://www.statcan.gc.ca/eng/start

Functions

StatcanR provides the R user with a consistent process to collect data from Statistics Canada’s data portal. It provides access to all Statistics Canada’ open economic data (formerly known as CANSIM tables) now identified by product IDs (PID) by the new Statistics Canada’s Web Data Service.

This tutorial presents how to use the statcanR R package and its function sqs_statcan_data(). The use of this package is separated into two parts. You first have to search the desired table, and then you are able to fetch the data from the sqs_statcan_data() function.

Some examples are provided below.

Search for data

In order to search for the desired information, Statistic Canada provides a search engine which indicates us the table number we are looking for. If we were interested in the federal expenditures on science and technology by socio-economic objectives, we would visit https://www150.statcan.gc.ca/n1/en/type/data?MM=1 and type in the search box the data’s description.

For this example the table number is ‘27-10-0014-01’. With the table number associated with our search, we can move on to extracting data with the API.

sqs_statcan_data()

The sqs_statcan_data() function takes as an input the table number obtained earlier and the data’s display language (french or english). The lang argument is either “fra” or “eng”.

For example, we can now extract the data associated with the federal expenditures on science and technology by socio-economic objectives.

#Loading the statCanR library
library(statcanR)

# Get data with sqs_statcan_data function
mydata <- sqs_statcan_data("27-10-0014-01", "eng")
REF_DATE GEO DGUID Type of expenditures Science and technology components Socio-economic objectives UOM UOM_ID SCALAR_FACTOR SCALAR_ID VECTOR COORDINATE VALUE STATUS SYMBOL TERMINATED DECIMALS INDICATOR
2002/2003 Canada 2016A000011124 Intramural Total science and technology Total socio-economic objectives Dollars 81 millions 6 v52460296 1.1.1.1 4271 NA 0 Federal expenditures on science and technology, by socio-economic objectives
2002/2003 Canada 2016A000011124 Intramural Total science and technology Exploration and exploitation of the earth Dollars 81 millions 6 v52460307 1.1.1.2 466 NA 0 Federal expenditures on science and technology, by socio-economic objectives
2002/2003 Canada 2016A000011124 Intramural Total science and technology Environment Dollars 81 millions 6 v52460313 1.1.1.7 359 NA 0 Federal expenditures on science and technology, by socio-economic objectives
2002/2003 Canada 2016A000011124 Intramural Total science and technology Exploration and exploitation of space Dollars 81 millions 6 v52460303 1.1.1.16 191 NA 0 Federal expenditures on science and technology, by socio-economic objectives
2002/2003 Canada 2016A000011124 Intramural Total science and technology Transport, telecommunications and other infrastructures Dollars 81 millions 6 v52460309 1.1.1.3 316 NA 0 Federal expenditures on science and technology, by socio-economic objectives
2002/2003 Canada 2016A000011124 Intramural Total science and technology Transport Dollars 81 millions 6 v52460310 1.1.1.4 112 NA t 0 Federal expenditures on science and technology, by socio-economic objectives

tl;dr

#Loading the statCanR library
library(statcanR)

# Get data with sqs_statcan_data function
mydata <- sqs_statcan_data("27-10-0014-01", "eng")

# List the first 5 social-economic objectives
mydata[1:5, 6:13]

Code learned this week

Command Detail
sqs_statcan_data() Extract data from Statistic Canada

References

This tutorial uses the statCanR package documentation.


Citation

For attribution, please cite this work as

Warin (2020, Jan. 30). Thierry Warin, PhD: [API] statcanR. Retrieved from https://warin.ca/posts/api-statcanr/

BibTeX citation

@misc{warin2020[api],
  author = {Warin, Thierry},
  title = {Thierry Warin, PhD: [API] statcanR},
  url = {https://warin.ca/posts/api-statcanr/},
  year = {2020}
}