API: statcanR

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

Table of Contents


By using SKEMA Quantum Studio framework (Warin 2019), this course will teach you how to use the statcanR package created by our team!

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 course uses the statCanR package documentation.


Warin, Thierry. 2019. “SKEMA Quantum Studio: A Technological Framework for Data Science in Higher Education.” https://doi.org/10.6084/m9.figshare.8204195.v2.

Citation

For attribution, please cite this work as

Warin (2020, Jan. 30). Virtual Campus: API: statcanR. Retrieved from https://virtualcampus.skemagloballab.io/posts/api-statcanr/

BibTeX citation

@misc{warin2020api:,
  author = {Warin, Thierry},
  title = {Virtual Campus: API: statcanR},
  url = {https://virtualcampus.skemagloballab.io/posts/api-statcanr/},
  year = {2020}
}