Skip to content
🇨🇭Search and Download Data from the Swiss Federal Statistical Office (https://felixluginbuhl.com/BFS)
R
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
R add donttest Oct 23, 2019
data-raw
docs
man
pkgdown/favicon
.Rbuildignore
.gitignore
.gitlab-ci.yml
BFS.Rproj
DESCRIPTION
LICENSE
NAMESPACE
README.Rmd
README.md
_pkgdown.yml

README.md

CRAN status Grand total pipeline status

BFS

Introduction

The BFS R package allows the user to search and download public data from the Swiss Federal Statistical Office (BFS) in a dynamic and reproducible way.

# install from CRAN
install.packages("BFS")

Caching and path gestion will be improved in the next BFS package release (for now only available on Github) using the pins R package. You can try the BFS development version by installing it from Github.

# install from Github
devtools::install_github("lgnbhl/BFS")

Usage Example

To search and download data from the BFS, you first need to retrieve information about the available datasets. The bfs_get_metadata() function returns a data frame/tibble containing the titles, publication dates, observation periods, website urls and download urls of all available BFS datasets of a given language: German (“de”, by default), French (“fr”), Italian (“it”) or English (“en”). Note that Italian and English languages give access to less datasets.

library(BFS)

meta_en <- bfs_get_metadata(language = "en")

To search for a dataset, you can use the bfs_search() function.

meta_en_edu <- bfs_search("education", data = meta_en)

print(meta_en_edu)
## # A tibble: 5 x 5
##   title            observation_period   published url             url_px        
##   <chr>            <chr>                <chr>     <chr>           <chr>         
## 1 Difficulties in… Observation period:… 26.11.20… https://www.bf… https://www.b…
## 2 Difficulties in… Observation period:… 26.11.20… https://www.bf… https://www.b…
## 3 University of a… Observation period:… 27.03.20… https://www.bf… https://www.b…
## 4 University of a… Observation period:… 27.03.20… https://www.bf… https://www.b…
## 5 Compulsory educ… Observation period:… 28.02.20… https://www.bf… https://www.b…

To download a BFS dataset, add the related url link from the url_px column of the downloaded metadata as an argument to the bfs_get_dataset() function.

df_edu <- bfs_get_dataset(meta_en_edu$url_px[3])

print(df_edu)
## # A tibble: 7,392 x 5
##    studienstufe     geschlecht isced_field                           jahr  value
##    <fct>            <fct>      <fct>                                 <fct> <dbl>
##  1 Diploma          Male       Education science                     1997      0
##  2 Bachelor         Male       Education science                     1997      0
##  3 Master           Male       Education science                     1997      0
##  4 Further educati… Male       Education science                     1997      0
##  5 Diploma          Female     Education science                     1997      0
##  6 Bachelor         Female     Education science                     1997      0
##  7 Master           Female     Education science                     1997      0
##  8 Further educati… Female     Education science                     1997      0
##  9 Diploma          Male       Teacher training without subject spe… 1997      0
## 10 Bachelor         Male       Teacher training without subject spe… 1997      0
## # … with 7,382 more rows

In case the url_px link to download the PC-Axis file is broken, you can have a look at its related BFS webpage using the url link.

browseURL(meta_en_edu$url[3]) # open webpage

Sometimes the PC-Axis file of the dataset doesn’t exist. You should then use the “STAT-TAB - interactive table” service provided by BFS to download manually the dataset.

To open the folder containing the downloaded BFS dataset (only available in the development version on Github for now), you can simply use the bfs_open_dir() function.

bfs_open_dir()

Other information

Alternative R package: pxweb

This package is in no way officially related to or endorsed by the Swiss Federal Statistical Office (BFS).

You can’t perform that action at this time.