Skip to content

Commit 4904ef6

Browse files
committed
- add aggregate_and_document_scale re #1
- update docs
1 parent b3ee533 commit 4904ef6

30 files changed

+1465
-1005
lines changed

NAMESPACE

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ S3method(summary,labelled)
55
S3method(summary,labelled_spss)
66
S3method(zap_attributes,data.frame)
77
S3method(zap_attributes,default)
8+
export(aggregate_and_document_scale)
89
export(asis_knit_child)
910
export(codebook)
1011
export(codebook_component_scale)

R/codebook.R

+9
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,15 @@ codebook_items <- function(results, indent = "##") {
273273
cache.path = paste0(knitr::opts_chunk$get("cache.path"), "overview_")
274274
)
275275
metadata_table <- codebook_table(results)
276+
metadata_table <- dplyr::mutate(metadata_table,
277+
name = paste0('<a href="#', safe_name(.data$name), '">',
278+
recursive_escape(.data$name), '</a>'))
279+
# bit ugly to suppress warnings here, but necessary for escaping whatever
280+
# columns there may be
281+
suppressWarnings(
282+
metadata_table <- dplyr::mutate_at(metadata_table, dplyr::vars(
283+
dplyr::one_of("label", "scale_item_names", "value_labels", "showif")),
284+
dplyr::funs(recursive_escape)) )
276285

277286
asis_knit_child(require_file("_codebook_items.Rmd"), options = options)
278287
}

R/correct_attributes.R

+43
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,46 @@ zap_label.default <- function(x) {
272272
attr(x, "label") <- NULL
273273
x
274274
}
275+
276+
277+
278+
#' Aggregate variables and remember which variables this were
279+
#'
280+
#' The resulting variables will have the attribute `scale_item_names` containing
281+
#' the basis for aggregation. Its `label` attribute will refer to the common stem of the
282+
#' aggregated variable names (if any), the number of variables, and the
283+
#' aggregation function.
284+
#'
285+
#' @param items data.frame of the items that should be aggregated
286+
#' @param fun aggregation function, defaults to rowMeans with na.rm = FALSE
287+
#' @param stem common stem for the variables, specify if it should not be auto-detected
288+
#' as the longest common stem of the variable names
289+
#' @export
290+
#' @examples
291+
#' testdf <- data.frame(bfi_neuro_1 = rnorm(20), bfi_neuro_2 = rnorm(20),
292+
#' bfi_neuro_3R = rnorm(20), age = rpois(20, 30))
293+
#' item_names <- c('bfi_neuro_1', 'bfi_neuro_2', 'bfi_neuro_3R')
294+
#' testdf$bfi_neuro <- aggregate_and_document_scale(testdf[, item_names])
295+
#' testdf$bfi_neuro
296+
aggregate_and_document_scale = function(items, fun = rowMeans, stem = NULL) {
297+
new_scale <- fun(items)
298+
item_names <- names(items)
299+
attributes(new_scale)$scale_item_names <- item_names
300+
301+
# find longest common stem
302+
if (is.null(stem)) {
303+
max_len <- min(nchar(item_names))
304+
for (l in max_len:0) {
305+
stem <- unique(stringr::str_sub(item_names, 1, l))
306+
if (length(stem) == 1) break;
307+
}
308+
}
309+
# string trimming for idiots
310+
if (nchar(stem)) {
311+
stem <- stringr::str_match(stem, "^(.+?)_?$")[, 2]
312+
}
313+
314+
attributes(new_scale)$label <- paste(ncol(items), stem, "items aggregated by",
315+
deparse(substitute(fun)))
316+
new_scale
317+
}

_pkgdown.yml

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ reference:
2020
- '`detect_scales`'
2121
- '`compute_reliabilities`'
2222
- '`rescue_attributes`'
23+
- '`aggregate_and_document_scale`'
2324
- title: Prepare data for non-codebook stuff
2425
desc: Functions to remove attributes that might confuse other packages
2526
contents:

docs/articles/codebook.html

+19-30
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Loading
Loading
Loading
Loading
Loading
Loading

docs/reference/aggregate_and_document_scale.html

+166
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)