-
Notifications
You must be signed in to change notification settings - Fork 23
/
README.Rmd
250 lines (171 loc) · 10.7 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
---
output: github_document
editor_options:
chunk_output_type: console
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.retina = 2,
fig.path = "man/figures/README-"
)
version <- as.vector(read.dcf("DESCRIPTION")[, "Version"])
version <- gsub("-", ".", version)
```
# readabs <img src="man/figures/logo.png" align="right" height="139" />
<!-- badges: start -->
[![R build status](https://github.com/mattcowgill/readabs/workflows/R-CMD-check/badge.svg)](https://github.com/mattcowgill/readabs/actions)
[![codecov status](https://img.shields.io/codecov/c/github/mattcowgill/readabs.svg)](https://app.codecov.io/gh/MattCowgill/readabs)
[![CRAN status](https://www.r-pkg.org/badges/version/readabs)](https://cran.r-project.org/package=readabs)
[![Lifecycle: stable](https://img.shields.io/badge/lifecycle-stable-brightgreen.svg)](https://lifecycle.r-lib.org/articles/stages.html)
<!-- badges: end -->
## Overview
{readabs} helps you easily download, import, and tidy data from the Australian Bureau of Statistics within R.
This saves you time manually downloading and tediously tidying data and allows you to spend more time on your analysis.
## Installing {readabs}
Install the latest CRAN version of {readabs} with:
```{r cran-installation, eval = FALSE}
install.packages("readabs")
```
You can install the development version of {readabs} from GitHub with:
```{r gh-installation, eval = FALSE}
# if you don't have devtools installed, first run:
# install.packages("devtools")
devtools::install_github("mattcowgill/readabs")
```
## Using {readabs}
The ABS releases data in many different formats, through many different dissemination channels.
The {readabs} contains functions for working with three different types of ABS data:
- `read_abs()` and related functions downloads, imports, and tidies ABS time series data.
- `download_abs_data_cube()` and related functions find and download ABS data cubes, which
are spreadsheets on the ABS website that are not in the standard time series format.
- `read_api()` and related functions find, filter, and import data from the [ABS.Stat](https://explore.data.abs.gov.au) API.
### ABS time series data
A key function in {readabs} is `read_abs()`, which downloads, imports, and tidies time series data from the ABS website. **Note that `read_abs()` only works with spreadsheets in the standard ABS time series format.**
First we'll load {readabs} and the {tidyverse}:
```{r load-package-dev, results = FALSE, warning = FALSE, eval = TRUE, include = FALSE}
devtools::load_all()
library(tidyverse)
```
```{r load-packages, results=FALSE, warning=FALSE, eval = FALSE}
library(readabs)
library(tidyverse)
```
Now we'll create one data frame that contains all the time series data from the Wage Price Index, catalogue number 6345.0:
```{r all-wpi}
all_wpi <- read_abs("6345.0")
```
This is what it looks like:
```{r str-wpi}
str(all_wpi)
```
It only takes you a few lines of code to make a graph from your data:
```{r all-in-one-example}
all_wpi %>%
filter(
series == "Percentage Change From Corresponding Quarter of Previous Year ; Australia ; Total hourly rates of pay excluding bonuses ; Private and Public ; All industries ;",
!is.na(value)
) %>%
ggplot(aes(x = date, y = value, col = series_type)) +
geom_line() +
theme_minimal() +
labs(y = "Annual wage growth (per cent)")
```
In the example above we downloaded all the time series from a catalogue number. This will often be overkill. If you know the data you need is in a particular table, you can just get that table like this:
```{r wpi1}
wpi_t1 <- read_abs("6345.0", tables = 1)
```
If you want multiple tables, but not the whole catalogue, that's easy too:
```{r wpi1_5}
wpi_t1_t5 <- read_abs("6345.0", tables = c("1", "5a"))
```
For more examples, please see the vignette on working with time series data (run `browseVignettes("readabs")`).
Some other functions that may come in handy when working with ABS time series data:
* `read_abs_local()` imports and tidies time series data from ABS spreadsheets stored on a local drive. Thanks to Hugh Parsonage for contributing to this functionality.
* `separate_series()` splits the `series` column of a tidied ABS time series spreadsheet into multiple columns, reducing the manual wrangling that's needed to work with the data. Thanks to David Diviny for writing this function.
#### Convenience functions for loading time series data
There are several functions that load specific ABS time series data:
* `read_cpi()` imports the Consumer Price Index numbers as a two-column tibble: `date` and `cpi`. This is useful for joining to other series to adjust data for changes in consumer prices.
* `read_awe()` returns a long time series of Average Weekly Earnings data.
* `read_job_mobility()` downloads, imports and tidies tables from the ABS Job Mobility dataset.
### ABS data cubes
The ABS (generally) releases time series data in a standard format, which allows `read_abs()` to download, import and tidy it (see above). But not all ABS data is time series data - the ABS also releases data as 'data cubes'. These are all formatted in their own, unique way.
Unfortunately, because data cubes are all formatted in their own way, there is no one function that can import tidy data cubes for you in the same way that `read_abs()` works with all time series. But `{readabs}` still has functions that can help. Thanks to David Diviny for writing these functions.
The `download_abs_data_cube()` function can download an ABS data cube for you. It works with any data cube on the ABS website. To use this function, we need two things: a `catalogue_string` (the short name of the release) and `cube`, a (unique fragment of) the filename within the catalogue you wish to download.
For example, let's say you wanted to download table 4 from _Weekly Payroll Jobs and Wages in Australia_. We can find the catalogue name like this:
```{r cat-name}
search_catalogues("payroll")
```
Now we know that the string `"weekly-payroll-jobs"` is the `catalogue_string` for this release. We can now see what files are available to download from this catalogue:
```{r files}
show_available_files("weekly-payroll-jobs")
```
We want Table 4, which has the filename `6160055001_DO004.xlsx`.
We can download the file as follows:
```{r download-data-cube}
payrolls_t4_path <- download_abs_data_cube("weekly-payroll-jobs", "004")
payrolls_t4_path
```
The `download_abs_data_cube()` function downloads the file and returns the full file path to the saved file. You can then pipe that in to another function:
```{r read-payrolls-manual, eval = FALSE}
payrolls_t4_path %>%
readxl::read_excel(
sheet = "Payroll jobs index",
skip = 5
)
```
#### Convenience functions for data cubes
As it happens, if you want the ABS Weekly Payrolls data, you don't need to use `download_abs_data_cube()` directly. Instead, there is a convenience function available that downloads, imports, and tidies the data for you:
```{r read-payrolls-fn, eval = FALSE}
read_payrolls()
```
There is also a convenience function available for data cube GM1 from the monthly Labour Force data, which contains labour force gross flows:
```{r read-lfs-grossflows, eval = FALSE}
read_lfs_grossflows()
```
### Finding and loading data from the ABS.Stat API
The ABS has created a new site to access its data, called the ABS Data Explorer, also known as ABS.Stat. As at early 2023, this site is in Beta mode. The site provides an API.
The {readabs} package includes functions to query the ABS.Stat API. Thank you to Kinto Behr for writing these functions. The functions are:
* `read_api_dataflows()` lists available dataflows (roughly equivalent to 'tables')
* `read_api_datastructure()` lists variables within a particular dataflow and the levels of those variables, which you can use to filter the data server-side in an API query
* `read_api()` downloads data from the ABS.Stat API.
Let's list available dataflows:
```{r api-flows}
flows <- read_api_dataflows()
```
Say from this I am interested in the first dataflow, the projected population of
Aboriginal and Torres Strait Islander Australians. The id for this dataflow is
`"ABORIGINAL_POP_PROJ"`, which I can use to download the data.
In this case, I could download the entire dataflow with:
```{r all-aboriginal-pop}
read_api("ABORIGINAL_POP_PROJ")
```
Let's say I'm only interested in the population projections for males, not females or all persons. In that case, I can filter the data on the ABS server before downloading my query. I can use `read_api_datastructure()` to help with this.
```{r datastructure}
read_api_datastructure("ABORIGINAL_POP_PROJ")
```
From this, I can see that there's a variable (`var`) called `sex_abs`, which can take the value `1`, `2`, or `3`, corresponding to `Males`, `Females` and `Persons`. If I only want to data for Males, I can obtain this by supplying a datakey:
```{r}
read_api("ABORIGINAL_POP_PROJ", datakey = list(sex_abs = 1))
```
Note that in some cases, querying the API without filtering the data will return an error, as the table will be too big. In this case, you will need to supply a datakey that reduces the size of the data.
## Resolving network issues by manually setting the download method
Certain corporate networks restrict your ability to download files in an R session. On some of these networks, the `"wininet"` method must be used when downloading files. Users can now specify the method that will be used to download files by setting the `"R_READABS_DL_METHOD"` environment variable.
For example, the following code sets the environment variable for your current session:
```{r, eval = FALSE}
Sys.setenv("R_READABS_DL_METHOD" = "wininet")
```
You can add `R_READABS_DL_METHOD = "wininet"` to your .Renviron to have this persist across sessions.
If you have other issues using `{readabs}` in your corporate environment, I would appreciate you opening an issue on GitHub.
## Bug reports and feedback
GitHub issues containing error reports or feature requests are welcome. Please try to make a [reprex](https://reprex.tidyverse.org) (a minimal, reproducible example) if possible.
Alternatively you can email the package maintainer at mattcowgill at gmail dot com.
## Disclaimer
The `{readabs}` package is not associated with the Australian Bureau of Statistics.
All data is provided subject to any restrictions and licensing arrangements
noted on the ABS website.
## Awesome Official Statistics Software
[![Mentioned in Awesome Official Statistics ](https://awesome.re/mentioned-badge.svg)](https://github.com/SNStatComp/awesome-official-statistics-software)
We're pleased to be included in a [list of software](https://github.com/SNStatComp/awesome-official-statistics-software) that can be used to work with official statistics.