Skip to content

Commit

Permalink
DS-1476 bugfix labels when as.binary = as.numeric = TRUE
Browse files Browse the repository at this point in the history
  • Loading branch information
mwmclean committed Sep 11, 2017
1 parent 23d678b commit 31c5089
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
16 changes: 9 additions & 7 deletions R/tidyrawdata.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ TidyRawData <- function(data,

if (missing(data) || !length(data))
stop("No data supplied")
## Search for common prefix for labels
if (extract.common.lab.prefix)
labs <- ExtractCommonPrefixFromLabels(data, tidy = TRUE)

## handle variables of QDate class
## data.frame ensures ProcessQVariables also returns a data.frame
Expand Down Expand Up @@ -113,10 +110,15 @@ TidyRawData <- function(data,
error.if.insufficient.obs = error.if.insufficient.obs)
data <- processed.data$estimation.data

## update labels and add common prefix attribute
if (extract.common.lab.prefix && !is.na(labs$common.prefix)){
attr(data, "label.prefix") <- labs$common.prefix
Labels(data) <- labs$shortened.labels
## Search for common prefix for labels
if (extract.common.lab.prefix)
{
labs <- ExtractCommonPrefixFromLabels(data, tidy = TRUE)
if (!is.na(labs$common.prefix)) ## update labels and add common prefix attribute
{
attr(data, "label.prefix") <- labs$common.prefix
Labels(data) <- labs$shortened.labels
}
}

attr(data, "weights") <- processed.data$weights
Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/test-tidyrawdata.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ test_that("TidyRawData: extracts label prefix and simplifies labels",
out <- suppressWarnings(TidyRawData(x, as.numeric = FALSE,
extract.common.lab.prefix = FALSE))
expect_equal(flipFormat::Labels(out), original.labels, check.attributes = FALSE)

## Okay with as.binary = as.numeric = TRUE creating extra vars
blabs <- as.vector(sapply(x, function(col) paste0(Labels(col), ": ", levels(col))))
out <- suppressWarnings(TidyRawData(x, as.numeric = TRUE, as.binary = TRUE,
extract.common.lab.prefix = FALSE))
expect_equal(flipFormat::Labels(out), blabs, check.attributes = FALSE)
})

################################################################################
Expand Down

0 comments on commit 31c5089

Please sign in to comment.