vignettes/cbsodataR.Rmd
cbsodataR.Rmd
Statistics Netherlands (CBS) is the office that produces all official statistics of the Netherlands.
For long SN has put its data on the web in its online database StatLine. Since 2014 this data base has an open data web API based on the OData protocol. The cbsodataR package allows for retrieving data right into R.
A list of tables can be retrieved using the cbs_get_toc
function.
library(dplyr) # not needed, but used in examples below
library(cbsodataR)
toc <- cbs_get_toc(Language="en") # retrieve only english tables
toc %>%
select(Identifier, ShortTitle)
## # A tibble: 777 x 2
## Identifier ShortTitle
## <chr> <chr>
## 1 80783eng Agriculture; general farm type, region
## 2 80784eng Agriculture; labour force, region
## 3 7100eng Arable crops; production
## 4 70671ENG Fruit culture; area fruit orchards
## 5 37738ENG Vegetables; yield per kind of vegetable
## 6 71509ENG Yield apples and pears, 1997 - 2017
## 7 83981ENG Livestock manure; key figures
## 8 80274eng Livestock cattle
## 9 7373eng Livestock pigs
## 10 7425eng Milk supply and dairy production
## # … with 767 more rows
Using an “Identifier” from cbs_get_toc
information on the table can be retrieved with cbs_get_meta
## 71509ENG: 'Yield apples and pears, 1997 - 2017', 2017
## FruitFarmingRegions: 'Fruit farming regions'
## Periods: 'Periods'
##
## Retrieve a default data selection with:
## cbs_get_data(id = "71509ENG", FruitFarmingRegions = c("1", "2",
## "4", "3", "5"), Periods = c("1997JJ00", "2012JJ00", "2013JJ00",
## "2016JJ00"), select = c("FruitFarmingRegions", "Periods", "TotalAppleVarieties_1",
## "CoxSOrangePippin_2", "DelbarestivaleDelcorf_3", "Elstar_4",
## "GoldenDelicious_5", "Jonagold_6", "Jonagored_7", "RodeBoskoopRennetApple_10",
## "OtherAppleVarieties_12", "TotalPearVarieties_13", "Conference_15",
## "DoyenneDuComice_16", "CookingPears_17", "TriompheDeVienne_18",
## "OtherPearVarieties_19", "TotalAppleVarieties_20", "CoxSOrangePippin_21",
## "DelbarestivaleDelcorf_22", "Elstar_23", "GoldenDelicious_24",
## "Jonagold_25", "Jonagored_26", "RodeBoskoopRennetApple_29", "OtherAppleVarieties_31",
## "TotalPearVarieties_32", "Conference_34", "DoyenneDuComice_35",
## "CookingPears_36", "TriompheDeVienne_37", "OtherPearVarieties_38"
## ))
The meta object contains all metadata properties of cbsodata (see the original documentation) in the form of data.frames. Each data.frame describes properties of the SN table.
## [1] "TableInfos" "DataProperties" "CategoryGroups"
## [4] "FruitFarmingRegions" "Periods"
With cbs_get_data
data can be retrieved. By default all data for this table will be downloaded in a temporary directory.
## # A tibble: 6 x 4
## FruitFarmingRegions Periods TotalAppleVarieties_1 CoxSOrangePippin_2
## <chr> <chr> <int> <int>
## 1 1 1997JJ00 420 43
## 2 1 1998JJ00 518 40
## 3 1 1999JJ00 568 39
## 4 1 2000JJ00 461 27
## 5 1 2001JJ00 408 30
## 6 1 2002JJ00 354 17
The opendata portal of CBS (https://opendata.cbs.nl/dataportaal/#/CBS/en) allows for finding a table and making a selection within this table manually. Such a selection can be stored in a hyperlink (click the “share” button). This link can also be used with cbsodataR
using the cbs_get_data_from_link
function.
cbs_get_data_from_link("https://opendata.cbs.nl/dataportaal/#/CBS/en/dataset/71509ENG/table?dl=193CB") %>%
select(1:4) %>%
head()
## Executing:
## cbs_get_data(id = "71509ENG", FruitFarmingRegions = c("1", "2", "3", "4", "5"), Periods = c("1997JJ00", "2012JJ00", "2013JJ00", "2016JJ00"), select = c("FruitFarmingRegions", "Periods", "TotalAppleVarieties_1", "CoxSOrangePippin_2", "DelbarestivaleDelcorf_3", "Elstar_4", "GoldenDelicious_5", "Jonagold_6", "Jonagored_7", "Junami_8", "Kanzi_9", "RodeBoskoopRennetApple_10", "Rubens_11", "OtherAppleVarieties_12", "TotalAppleVarieties_20", "CoxSOrangePippin_21", "DelbarestivaleDelcorf_22", "Elstar_23", "GoldenDelicious_24", "Jonagold_25", "Jonagored_26", "Junami_27", "Kanzi_28", "RodeBoskoopRennetApple_29", "Rubens_30", "OtherAppleVarieties_31"), base_url = "http://opendata.cbs.nl")
## # A tibble: 6 x 4
## FruitFarmingRegions Periods TotalAppleVarieties_1 CoxSOrangePippin_2
## <chr> <chr> <int> <int>
## 1 1 1997JJ00 420 43
## 2 1 2012JJ00 282 3
## 3 1 2013JJ00 314 4
## 4 1 2016JJ00 317 2
## 5 2 1997JJ00 86 10
## 6 2 2012JJ00 34 0
The first columns are categorical columns: they contain codes. The labels for these columns can be added with the function cbs_add_label_columns
.
## # A tibble: 6 x 4
## FruitFarmingRegions FruitFarmingRegions_label Periods Periods_label
## <chr> <fct> <chr> <fct>
## 1 1 Total Netherlands 1997JJ00 1997
## 2 1 Total Netherlands 1998JJ00 1998
## 3 1 Total Netherlands 1999JJ00 1999
## 4 1 Total Netherlands 2000JJ00 2000
## 5 1 Total Netherlands 2001JJ00 2001
## 6 1 Total Netherlands 2002JJ00 2002
The period/time columns of Statistics Netherlands (CBS) contain coded time periods: e.g. 2018JJ00 (i.e. 2018), 2018KW03 (i.e. 2018 Q3), 2016MM04 (i.e. 2016 April). With cbs_add_date_column
the time periods will be converted and added to the data:
## # A tibble: 6 x 3
## Periods Periods_Date Periods_freq
## <chr> <date> <fct>
## 1 1997JJ00 1997-01-01 Y
## 2 1998JJ00 1998-01-01 Y
## 3 1999JJ00 1999-01-01 Y
## 4 2000JJ00 2000-01-01 Y
## 5 2001JJ00 2001-01-01 Y
## 6 2002JJ00 2002-01-01 Y
This can be useful for further time series analysis, but also for displaying. It is also possible to convert the dates to numbers:
## # A tibble: 6 x 3
## Periods Periods_numeric Periods_freq
## <chr> <int> <fct>
## 1 1997JJ00 1997 Y
## 2 1998JJ00 1998 Y
## 3 1999JJ00 1999 Y
## 4 2000JJ00 2000 Y
## 5 2001JJ00 2001 Y
## 6 2002JJ00 2002 Y
It is possible restrict the download using filter statements. This may shorten the download time considerably.
## # A tibble: 6 x 4
## FruitFarmingRegions Periods TotalAppleVarieties_1 CoxSOrangePippin_2
## <chr> <chr> <int> <int>
## 1 1 2000JJ00 461 27
## 2 1 2001JJ00 408 30
## 3 2 2000JJ00 87 5
## 4 2 2001JJ00 75 5
## 5 4 2000JJ00 105 10
## 6 4 2001JJ00 87 9