Skip to content

Commit 819185a

Browse files
committed
added cbs_add_unit_column
1 parent f43f1f9 commit 819185a

19 files changed

+120
-1
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ Suggests:
2323
testthat (>= 2.1.0),
2424
sf
2525
VignetteBuilder: knitr
26-
RoxygenNote: 7.3.1
26+
RoxygenNote: 7.3.2
2727
Roxygen: list(markdown = TRUE)

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export(cache_clear)
1313
export(cbs_add_date_column)
1414
export(cbs_add_label_columns)
1515
export(cbs_add_statcode_column)
16+
export(cbs_add_unit_column)
1617
export(cbs_download_data)
1718
export(cbs_download_meta)
1819
export(cbs_download_table)

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
* Bug fix for issue #37 cbs_download_data: catalog error, thanks to @guyhill for reporting
88

9+
* Added `cbs_add_unit_column` to add unit columns to the data set, thanks to Marieke Rensman en Martin van Elp for the suggestion
10+
911
# cbsodataR 1.0.1
1012

1113
* fixed example in `cbs_get_catalogs`, which failed when catalog was temporarily

R/add_column_labels.R

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,21 @@ add_var_labels <- function(x, meta){
1212
x
1313
}
1414

15+
16+
# utility function adding units to columns
17+
add_var_units <- function(x, meta){
18+
props <- meta$DataProperties
19+
units <- stats::setNames(props$Unit, props$Key)
20+
for (n in names(x)){
21+
unit <- unname(units[n])
22+
if (is.na(unit) || is.null(unit)){
23+
next
24+
}
25+
attr(x[[n]], "unit") <- unname(units[n])
26+
}
27+
x
28+
}
29+
1530
#View(add_label(x, meta))
1631
#x <- get_cbs_data("81819NED")
1732
#meta <- get_meta("81819NED", verbose = FALSE)

R/cbs_add_unit_column.R

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#' For each topic column add a unit column
2+
#'
3+
#' Adds extra unit columns to the dataset that was retrieved using [cbs_get_data()].
4+
#'
5+
#' The unit columns will be named `<topic_column>_unit`, and are a `character`
6+
#'
7+
#' By default all code columns will be accompagnied with a label column. The name
8+
#' of each label column will be `<code_column>_label`.
9+
#' @export
10+
#' @param x `data.frame` retrieved using [cbs_get_data()].
11+
#' @param columns `character` with the names of the columns for which labels will be added
12+
#' @param ... not used.
13+
#' @return the original data.frame `x` with extra unit
14+
#' columns. (see description)
15+
#' @family data retrieval
16+
#' @family meta data
17+
cbs_add_unit_column <- function(x, columns=colnames(x), ...){
18+
add <- list()
19+
nms <- colnames(x)
20+
N <- nrow(x)
21+
22+
for (n in columns){
23+
values <- x[[n]]
24+
25+
if (is.null(values)){
26+
warning("Column '", n, "' does not exist.")
27+
next
28+
}
29+
30+
unit <- attr(x[[n]], "unit")
31+
if (is.null(unit)){
32+
next
33+
}
34+
un <- paste0(n, "_unit")
35+
x[[un]] <- rep(unit, N)
36+
add[[n]] <- un
37+
}
38+
# rearrange column order
39+
cols <- unlist(lapply(nms, function(n){c(n, add[[n]])}))
40+
x[,cols]
41+
}

R/cbs_get_data.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ cbs_get_data <- function( id
8888

8989
if (add_column_labels){
9090
data <- add_var_labels(data, meta)
91+
data <- add_var_units(data, meta)
9192
}
9293

9394
is_time <- meta$DataProperties$Key[meta$DataProperties$Type == "TimeDimension"]

man/cbs_add_date_column.Rd

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/cbs_add_label_columns.Rd

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/cbs_add_unit_column.Rd

Lines changed: 45 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/cbs_download_data.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/cbs_download_meta.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/cbs_extract_table_id.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/cbs_get_data.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/cbs_get_data_from_link.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/cbs_get_meta.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/download_data-deprecated.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/download_meta-deprecated.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/get_data-deprecated.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/get_meta-deprecated.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)