rOpenGov / pxweb

R tools to access PX-WEB API
http://ropengov.github.io/pxweb
Other
69 stars 31 forks source link

Use aggregation in Query #181

Open larpSTATGL opened 5 years ago

larpSTATGL commented 5 years ago

In a pxweb based statbank system defined aggregations can be used in a JSON query. You can locate relevant aggregations on your chosen site, by looking to "About table" - "API query for this table" after viewing a table on screen with selected aggregation

First the standard way - notice "*" (all) for age:

From the Greenlandic Statbank this query extract the female, greenlandic born population on 1-year age groups:

px_data_female <- pxweb_get(url = "http://bank.stat.gl/api/v1/en/Greenland/BE/BE01/BE0120/BEXST1.PX", query = list("age"=c(""), "gender"=c("2"), "place of birth"=c("N"), "time"=c("")))

NEXT use and compare agg-file and you get the aggregation in return:

if(!require(pxweb)){install.packages("pxweb")} library(pxweb)

q_f <- '{"query": [{"code": "age","selection": {"filter": "agg:5-year.agg", "values": ["-4","5-9","10-14","15-19","20-24","25-29","30-34","35-39","40-44","45-49","50-54","55-59","60-64","65-69","70-74","75-79","80-84","85-89","90-94","95+"]}},{"code": "place of birth","selection": {"filter": "item","values": ["N"]}},{"code": "gender","selection": {"filter": "item","values": ["2"]}},{"code": "time","selection": {"filter": "all","values": ["*"]}}],"response": {"format": "px"}}' px_data_female <-

pxweb_get(url = "http://bank.stat.gl/api/v1/en/Greenland/BE/BE01/BE0120/BEXST1.PX", query = q_f)

px_data_female <- as.data.frame(px_data_female, column.name.type = "text", variable.value.type = "code")

matrix_female <- matrix(data = px_data_female$Population January 1st, ncol = 43, byrow = TRUE )

MansMeg commented 5 years ago

Thanks! Ill put this in the vignette later on.