# BFS > Search and download data from the Swiss Federal Statistical Office BFS gives you the open data of the [Swiss Federal Statistical Office](https://www.bfs.admin.ch/bfs/en/home/statistics/catalogue.html) (BFS, *Bundesamt für Statistik*) in R. You get tidy data frames, in German, French, Italian or English. The package works with the four APIs of the BFS: | You want | Use | API | |----|----|----| | Statistics of the Swiss Stats Explorer | [`bfs_sse_datasets()`](https://felixluginbuhl.com/BFS/reference/bfs_sse_datasets.md), [`bfs_sse_dimensions()`](https://felixluginbuhl.com/BFS/reference/bfs_sse_dimensions.md), [`bfs_sse_data()`](https://felixluginbuhl.com/BFS/reference/bfs_sse_data.md) | [Swiss Stats Explorer](https://stats.swiss/) | | Datasets of the BFS data catalog | [`bfs_get_catalog_data()`](https://felixluginbuhl.com/BFS/reference/bfs_get_catalog_data.md), [`bfs_get_data()`](https://felixluginbuhl.com/BFS/reference/bfs_get_data.md) | [PXWeb](https://www.pxweb.bfs.admin.ch/pxweb/en/) | | Tables and files (Excel, CSV, PX, …) | [`bfs_get_catalog_tables()`](https://felixluginbuhl.com/BFS/reference/bfs_get_catalog_tables.md), [`bfs_download_asset()`](https://felixluginbuhl.com/BFS/reference/bfs_download_asset.md) | [DAM](https://dam-api.bfs.admin.ch/hub/swagger-ui/index.html) | | Maps and geodata | [`bfs_get_base_maps()`](https://felixluginbuhl.com/BFS/reference/bfs_get_base_maps.md), [`bfs_get_catalog_geodata()`](https://felixluginbuhl.com/BFS/reference/bfs_get_catalog_geodata.md), [`bfs_download_geodata()`](https://felixluginbuhl.com/BFS/reference/bfs_download_geodata.md) | [ThemaKart](https://www.bfs.admin.ch/bfs/en/home/statistics/regional-statistics/base-maps/cartographic-bases.html), [STAC](https://data.geo.admin.ch/api/stac/v0.9/) | The BFS is replacing STAT-TAB (the platform of the PXWeb API) with the Swiss Stats Explorer. See [Get started](https://felixluginbuhl.com/BFS/articles/introduction.html#from-stat-tab-to-the-swiss-stats-explorer). [Get started](https://felixluginbuhl.com/BFS/articles/introduction.html) [Swiss Stats Explorer](https://felixluginbuhl.com/BFS/articles/swiss-stats-explorer.html) [Tables and files](https://felixluginbuhl.com/BFS/articles/tables-and-files.html) ## Installation ``` r install.packages("BFS") # or the development version # install.packages("remotes") remotes::install_github("lgnbhl/BFS") ``` ## Quick tour ``` r library(BFS) ``` ### Swiss Stats Explorer Let’s get the vacancy rate (the share of empty homes, in %) of the cantons of Geneva, Zurich and Bern. The dataset is `DF_LWZ_1`, the vacant dwellings. You find this id with [`bfs_sse_datasets()`](https://felixluginbuhl.com/BFS/reference/bfs_sse_datasets.md), or in the URL of the dataset on [stats.swiss](https://stats.swiss/). First, see the dimensions of the dataset and the codes of their values. Then download the data with the codes you need: ``` r dimensions <- bfs_sse_dimensions("DF_LWZ_1", language = "en") dimensions ``` ``` R ## # A tibble: 2,317 × 6 ## dimension dimension_label value value_label position parent ## ## 1 GR_KT_GDE Cantons, districts and communes 1 Aeugst am Al… 1 B_101 ## 2 GR_KT_GDE Cantons, districts and communes 10 Obfelden 1 B_101 ## 3 GR_KT_GDE Cantons, districts and communes 100 Stadel 1 B_104 ## 4 GR_KT_GDE Cantons, districts and communes 1001 Doppleschwand 1 B_316 ## 5 GR_KT_GDE Cantons, districts and communes 1002 Entlebuch 1 B_316 ## 6 GR_KT_GDE Cantons, districts and communes 1004 Flühli 1 B_316 ## 7 GR_KT_GDE Cantons, districts and communes 1005 Hasle (LU) 1 B_316 ## 8 GR_KT_GDE Cantons, districts and communes 1007 Romoos 1 B_316 ## 9 GR_KT_GDE Cantons, districts and communes 1008 Schüpfheim 1 B_316 ## 10 GR_KT_GDE Cantons, districts and communes 1009 Werthenstein 1 B_316 ## # ℹ 2,307 more rows ``` ``` r bfs_sse_data( "DF_LWZ_1", filters = list( GR_KT_GDE = c("GE", "ZH", "BE"), # the cantons of Geneva, Zurich and Bern WOHN_ANZAHL = "_T", # all numbers of rooms LEERWOHN_TYP = "_T", # all types of vacant dwellings MEASURE_DIMENSION = "RATE" # the vacancy rate, in % ), start_period = "2020", language = "en" ) ``` ``` R ## # A tibble: 21 × 12 ## GR_KT_GDE GR_KT_GDE_label WOHN_ANZAHL WOHN_ANZAHL_label LEERWOHN_TYP ## ## 1 ZH Zurich _T Total _T ## 2 ZH Zurich _T Total _T ## 3 ZH Zurich _T Total _T ## 4 ZH Zurich _T Total _T ## 5 ZH Zurich _T Total _T ## 6 ZH Zurich _T Total _T ## 7 ZH Zurich _T Total _T ## 8 BE Bern _T Total _T ## 9 BE Bern _T Total _T ## 10 BE Bern _T Total _T ## # ℹ 11 more rows ## # ℹ 7 more variables: LEERWOHN_TYP_label , MEASURE_DIMENSION , ## # MEASURE_DIMENSION_label , FREQ , FREQ_label , ## # TIME_PERIOD , value ``` ### Data catalog Search the BFS data catalog. Then download a dataset with its BFS number. ``` r bfs_get_catalog_data(language = "en", extended_search = "university students") ``` ``` R ## # A tibble: 4 × 6 ## title language number_bfs number_asset publication_date url_px ## ## 1 University of applie… en px-x-1502… 36454608 2026-03-24 https… ## 2 University of applie… en px-x-1502… 36454606 2026-03-24 https… ## 3 University students … en px-x-1502… 36454426 2026-03-24 https… ## 4 University students … en px-x-1502… 36454424 2026-03-24 https… ``` ``` r bfs_get_data(number_bfs = "px-x-1502040100_131", language = "en") ``` ``` R ## # A tibble: 19,320 × 5 ## Year `ISCED Field` Sex `Level of study` `University students` ## ## 1 1980/81 Education science Male First university degr… 545 ## 2 1980/81 Education science Male Bachelor 0 ## 3 1980/81 Education science Male Master 0 ## 4 1980/81 Education science Male Doctorate 93 ## 5 1980/81 Education science Male Further education, ad… 13 ## 6 1980/81 Education science Female First university degr… 946 ## 7 1980/81 Education science Female Bachelor 0 ## 8 1980/81 Education science Female Master 0 ## 9 1980/81 Education science Female Doctorate 70 ## 10 1980/81 Education science Female Further education, ad… 52 ## # ℹ 19,310 more rows ``` ### Tables and files The BFS publishes many statistics as Excel or CSV tables. Search them. Then download a file with its asset number. ``` r tables <- bfs_get_catalog_tables(language = "en", extended_search = "students") tables ``` ``` R ## # A tibble: 6 × 5 ## title language number_asset publication_date order_nr ## ## 1 Students at universities and … en 36460671 2026-03-24 ts-x-15… ## 2 Students at universities and … en 36460677 2026-03-24 su-e-15… ## 3 Students at universities of a… en 36460666 2026-03-24 ts-x-15… ## 4 Students at universities of a… en 36460675 2026-03-24 su-e-15… ## 5 Students at universities of t… en 36460673 2026-03-24 su-e-15… ## 6 Students at universities of a… en 7746958 2019-03-27 su-e-15… ``` ``` r bfs_download_asset(number_asset = tables$number_asset[1], destfile = "students.xlsx") ``` ### Maps Get the official borders of Switzerland, the cantons, the districts or the communes as sf objects. ``` r library(ggplot2) ggplot(bfs_get_base_maps(geom = "kant")) + geom_sf() + theme_void() ``` ## Learn more - [Get started](https://felixluginbuhl.com/BFS/articles/introduction.html): which API for which data, and the move from STAT-TAB to the Swiss Stats Explorer. - [Swiss Stats Explorer](https://felixluginbuhl.com/BFS/articles/swiss-stats-explorer.html): find a dataset, choose the data and download it, step by step. - [Data catalog and PXWeb datasets](https://felixluginbuhl.com/BFS/articles/pxweb.html): search the catalog and download all or part of a dataset. - [Tables and files](https://felixluginbuhl.com/BFS/articles/tables-and-files.html): download any BFS file. - [Geodata and base maps](https://felixluginbuhl.com/BFS/articles/geodata.html): maps of Switzerland and geodata of the Swiss geo-portal. - [API errors and rate limits](https://felixluginbuhl.com/BFS/articles/troubleshooting.html): what to do when an API is not available. ## Related packages - [pxweb](https://ropengov.github.io/pxweb/): the PXWeb client used by BFS. - [rstac](https://brazil-data-cube.github.io/rstac/): the STAC client used by [`bfs_download_geodata()`](https://felixluginbuhl.com/BFS/reference/bfs_download_geodata.md). - [swissMunicipalities](https://github.com/SwissStatsR/swissMunicipalities): the historicized list of Swiss communes. - [I14Y](https://felixluginbuhl.com/I14Y/): the metadata of the Swiss interoperability platform I14Y. ## Contributing BFS is not affiliated with the Swiss Federal Statistical Office. Bug reports, questions and pull requests are welcome on [GitHub](https://github.com/lgnbhl/BFS/issues). # Package index ## Swiss Stats Explorer The Swiss Stats Explorer () is the newer data API of the BFS, based on the SDMX standard. - [`bfs_sse_datasets()`](https://felixluginbuhl.com/BFS/reference/bfs_sse_datasets.md) : List the datasets of the Swiss Stats Explorer - [`bfs_sse_dimensions()`](https://felixluginbuhl.com/BFS/reference/bfs_sse_dimensions.md) : Get the dimensions of a Swiss Stats Explorer dataset - [`bfs_sse_data()`](https://felixluginbuhl.com/BFS/reference/bfs_sse_data.md) : Get data from the Swiss Stats Explorer ## Swiss Stats Explorer (deprecated) Deprecated since BFS 0.8.0, and kept unchanged for the code using them. Use the functions above, see the article “Moving to the new Swiss Stats Explorer functions”. - [`bfs_get_sse_metadata()`](https://felixluginbuhl.com/BFS/reference/bfs_get_sse_metadata.md) **\[deprecated\]** : Get the codelists of a Swiss Stats Explorer dataset (deprecated) - [`bfs_get_sse_data()`](https://felixluginbuhl.com/BFS/reference/bfs_get_sse_data.md) **\[deprecated\]** : Get data from the Swiss Stats Explorer (deprecated) ## Data catalog and PXWeb datasets Search the BFS data catalog, then download a dataset from the PXWeb API with its BFS number. - [`bfs_get_catalog_data()`](https://felixluginbuhl.com/BFS/reference/bfs_get_catalog_data.md) : Search the BFS data catalog - [`bfs_get_metadata()`](https://felixluginbuhl.com/BFS/reference/bfs_get_metadata.md) : Get the variables of a BFS dataset - [`bfs_get_data()`](https://felixluginbuhl.com/BFS/reference/bfs_get_data.md) : Get a BFS dataset - [`bfs_get_data_comments()`](https://felixluginbuhl.com/BFS/reference/bfs_get_data_comments.md) : Get the comments of a BFS dataset ## Tables and files Search the BFS tables catalog and download any BFS file (Excel, CSV, PX, …) from the DAM API. - [`bfs_get_catalog_tables()`](https://felixluginbuhl.com/BFS/reference/bfs_get_catalog_tables.md) : Search the BFS tables catalog - [`bfs_get_asset_metadata()`](https://felixluginbuhl.com/BFS/reference/bfs_get_asset_metadata.md) : Get the metadata of a BFS file - [`bfs_download_asset()`](https://felixluginbuhl.com/BFS/reference/bfs_download_asset.md) : Download a BFS file ## Geodata and base maps Geodata of the Swiss geo-portal (STAC API) and the cartographic base maps of the BFS ThemaKart project. - [`bfs_get_catalog_geodata()`](https://felixluginbuhl.com/BFS/reference/bfs_get_catalog_geodata.md) : Get the geodata catalog of the Swiss Confederation - [`bfs_download_geodata()`](https://felixluginbuhl.com/BFS/reference/bfs_download_geodata.md) : Download geodata from the Swiss geo-portal - [`bfs_get_base_maps()`](https://felixluginbuhl.com/BFS/reference/bfs_get_base_maps.md) : Get the Swiss base maps - [`bfs_clear_cache()`](https://felixluginbuhl.com/BFS/reference/bfs_clear_cache.md) : Clear the BFS cache ## Swiss official commune register The register of the Swiss communes, districts and cantons on 1 January 2023, included in the package. - [`register_bzn`](https://felixluginbuhl.com/BFS/reference/register_bzn.md) : Swiss official commune register: districts - [`register_dic`](https://felixluginbuhl.com/BFS/reference/register_dic.md) : Swiss official commune register: dictionary - [`register_gde`](https://felixluginbuhl.com/BFS/reference/register_gde.md) : Swiss official commune register: communes - [`register_gde_other`](https://felixluginbuhl.com/BFS/reference/register_gde_other.md) : Swiss official commune register: areas without a commune - [`register_kt`](https://felixluginbuhl.com/BFS/reference/register_kt.md) : Swiss official commune register: cantons - [`register_kt_seeanteile`](https://felixluginbuhl.com/BFS/reference/register_kt_seeanteile.md) : Swiss official commune register: cantonal shares of lakes ## Deprecated - [`bfs_get_catalog()`](https://felixluginbuhl.com/BFS/reference/bfs_get_catalog.md) : Get the BFS data or tables catalog (deprecated) # Articles ### Get started - [Get started](https://felixluginbuhl.com/BFS/articles/introduction.md): - [API errors and rate limits](https://felixluginbuhl.com/BFS/articles/troubleshooting.md): What to do when a BFS API is not available. ### Data - [Swiss Stats Explorer](https://felixluginbuhl.com/BFS/articles/swiss-stats-explorer.md): Find a dataset, choose the data you need and download it, step by step. - [Moving to the new Swiss Stats Explorer functions](https://felixluginbuhl.com/BFS/articles/sse-migration.md): Why BFS 0.8.0 has new functions for the Swiss Stats Explorer, how they are different from bfs_get_sse_data() and bfs_get_sse_metadata(), and how to change your code. - [Data catalog and PXWeb datasets](https://felixluginbuhl.com/BFS/articles/pxweb.md): Search the BFS data catalog and download all or part of a dataset. ### Files and maps - [Tables and files](https://felixluginbuhl.com/BFS/articles/tables-and-files.md): Search the BFS tables and download any BFS file. - [Geodata and base maps](https://felixluginbuhl.com/BFS/articles/geodata.md): Maps of Switzerland, its cantons, districts and communes, and the geodata of the Swiss geo-portal.