Skip to content

Commit

Permalink
Corrects handling of partially labelled data
Browse files Browse the repository at this point in the history
  • Loading branch information
DoctorBJones committed Apr 12, 2023
1 parent 35df84d commit 23cd513
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 30 deletions.
65 changes: 39 additions & 26 deletions R/utils.R
Expand Up @@ -230,41 +230,54 @@ times_summary <- function(dataset, column) {


label_summary <- function(dataset, column) {
label_values <-
as.data.frame(attributes(dataset[[column]])$labels) |>
rownames_to_column()

names(label_values)[1] <- "label"
names(label_values)[2] <- "value"
if (length(unique(dataset[[column]])) ==
length(attr(dataset[[column]], "labels"))) {

label_values$summary <-
paste(label_values$label, " (", label_values$value, ")",
sep = "")
label_values <-
as.data.frame(attributes(dataset[[column]])$labels) |>
tibble::rownames_to_column()

a <- as.data.frame(table(dataset[[column]]))
names(a)[1] <- "num_val"
names(a)[2] <- "value"
names(label_values)[1] <- "label"
names(label_values)[2] <- "value"

a <- merge(a, label_values, by.x = "num_val", by.y = "value")
label_values$summary <-
paste(label_values$label, " (", label_values$value, ")",
sep = "")

a$item <- ""
a$item[1] <- gsub('"', '', deparse(column))
a <- as.data.frame(table(dataset[[column]]))
names(a)[1] <- "num_val"
names(a)[2] <- "value"

a$class <- ""
a$class[1] <-
paste(class(dataset[[column]]), sep = " ", collapse = " ")
a <- merge(a, label_values, by.x = "num_val", by.y = "value")

a$label <- ""
a$label[1] <- ifelse(is.null(attr(dataset[[column]], "label")),
"No label", attr(dataset[[column]], "label"))
a$item <- ""
a$item[1] <- gsub('"', '', deparse(column))

vars <- c("item", "label", "class", "summary", "value")
a <- a[, vars]
a[nrow(a) + 1, ] <-
c("", "", "", "missing", sum(is.na(dataset[[column]])))
a$value <- as.character(a$value)
a$class <- ""
a$class[1] <-
paste(class(dataset[[column]]), sep = " ", collapse = " ")

return(a)
a$label <- ""
a$label[1] <- ifelse(is.null(attr(dataset[[column]], "label")),
"No label", attr(dataset[[column]], "label"))

vars <- c("item", "label", "class", "summary", "value")
a <- a[, vars]
a[nrow(a) + 1, ] <-
c("", "", "", "missing", sum(is.na(dataset[[column]])))
a$value <- as.character(a$value)

return(a)

} else {

msg <- paste0(column, " has different numbers of labels and levels. It has been treated as numeric")
warning(msg)

numeric_summary(dataset = dataset, column = column)

}

}

Expand Down
7 changes: 4 additions & 3 deletions tests/testthat/test-create_dictionary.R
Expand Up @@ -25,7 +25,7 @@ testthat::test_that("dictionary",{
)

testthat::expect_equal(over$value[2],
"14"
"15"
)

# dimensions of object
Expand All @@ -34,7 +34,7 @@ testthat::test_that("dictionary",{
id_var = "id")

testthat::expect_equal(nrow(len),
62
67
)

# id var properly summarised
Expand All @@ -59,7 +59,8 @@ testthat::test_that("dictionary",{
lab_location = "Location",
effective_date = "Date recorded",
all_missing = "Missing data",
time_recorded = "Time recorded"
time_recorded = "Time recorded",
labelled_data = "Labelled"
)

lab <- create_dictionary(
Expand Down
20 changes: 19 additions & 1 deletion tests/testthat/test-summarise_variable.R
Expand Up @@ -26,6 +26,14 @@ testthat::test_that("error", {
"bad_factor"),
"bad_factor has more than 10 levels, did you want a character variable?"
)

testthat::expect_warning(
summarise_variable(
readRDS(file = testthat::test_path("testdata", 'tester.rds')),
"bad_labels"),
"bad_labels has different numbers of labels and levels. It has been treated as numeric"
)

})

# test each data class
Expand Down Expand Up @@ -78,7 +86,17 @@ testthat::test_that("classes", {

testthat::expect_equal(
s$summary[3],
"Vic (4)"
"Qld (3)"
)

# haven partially labelled
p <- summarise_variable(
readRDS(file = testthat::test_path("testdata", 'tester.rds')),
"bad_labels")

testthat::expect_equal(
p$summary[1],
"mean"
)

# difftime
Expand Down
Binary file modified tests/testthat/testdata/tester.rds
Binary file not shown.
Binary file modified tests/testthat/testdata/tester_no_error.rds
Binary file not shown.

0 comments on commit 23cd513

Please sign in to comment.