Skip to content

Instantly share code, notes, and snippets.

@djhurio
Created March 29, 2021 10:32
Show Gist options
  • Save djhurio/ae764e250afba5198cf62d7af0f781a8 to your computer and use it in GitHub Desktop.
Save djhurio/ae764e250afba5198cf62d7af0f781a8 to your computer and use it in GitHub Desktop.
Piemērs datu importam no Oficiālās statistikas portāla, izmantojot API un R pakotni 'pxweb'
# Piemērs CSP datu importam ar pxweb
# Vairāk lasīt:
# https://htmlpreview.github.io/?https://github.com/rOpenGov/pxweb/blob/test/vignettes/pxweb.html
library(pxweb)
# API URL
px_url <- "https://data.stat.gov.lv/api/v1/lv/OSP_PUB/START/VEK/IK/IKP/IKP020"
# Metadati
px_meta <- pxweb_get(px_url)
# Mainīgo saraksts
px_code_list <- sapply(px_meta$variables, `[[`, "code")
# Izveido datu pieprasījumu visām vērtībām
# "*" nozīmē visas vērtības
pxweb_query_list <- as.list(rep("*", length(px_code_list)))
names(pxweb_query_list) <- px_code_list
pxq <- pxweb_query(pxweb_query_list)
# Datu pieprasījuma validācija
pxweb_validate_query_with_metadata(pxq, px_meta)
# Datu pieprasīšana
pxd <- pxweb_get(url = px_url, query = pxq)
pxd
# Pārveido uz data.frame ar tekstu vai kodiem
head(as.data.frame(pxd, column.name.type = "text", variable.value.type = "text"))
head(as.data.frame(pxd, column.name.type = "code", variable.value.type = "code"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment