Skip to content

Commit

Permalink
ensure that both functions return a grouped dataframe
Browse files Browse the repository at this point in the history
  • Loading branch information
etiennebacher committed Aug 21, 2023
1 parent 2cb4e64 commit 86478c2
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 24 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: datawizard
Title: Easy Data Wrangling and Statistical Transformations
Version: 0.8.0.7
Version: 0.8.0.8
Authors@R: c(
person("Indrajeet", "Patil", , "[email protected]", role = "aut",
comment = c(ORCID = "0000-0003-1995-6531", Twitter = "@patilindrajeets")),
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ CHANGES

* `datawizard` moves from the GPL-3 license to the MIT license.

* `unnormalize()` and `unstandardize()` now work with grouped data (#415).

BUG FIXES

* Fixed issues in `data_write()` when writing labelled data into SPSS format
Expand Down
21 changes: 12 additions & 9 deletions R/unnormalize.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ unnormalize.default <- function(x, ...) {
#' @rdname normalize
#' @export
unnormalize.numeric <- function(x, verbose = TRUE, ...) {

## TODO implement algorithm include_bounds = FALSE

# if function called from the "grouped_df" method, we use the dw_transformer
# attributes that were recovered in the "grouped_df" method

Expand All @@ -24,15 +27,14 @@ unnormalize.numeric <- function(x, verbose = TRUE, ...) {

if (!is.null(grp_attr_dw)) {
names(grp_attr_dw) <- gsub(".*\\.", "", names(grp_attr_dw))

include_bounds <- unname(grp_attr_dw["include_bounds"])
min_value <- unname(grp_attr_dw["min_value"])
range_difference <- unname(grp_attr_dw["range_difference"])
to_range <- unname(grp_attr_dw["to_range"])

if (is.na(to_range)) to_range <- NULL
include_bounds <- grp_attr_dw["include_bounds"]
min_value <- grp_attr_dw["min_value"]
range_difference <- grp_attr_dw["range_difference"]
to_range <- grp_attr_dw["to_range"]
if (is.na(to_range)) {
to_range <- NULL
}
} else {
## TODO implement algorithm include_bounds = FALSE
include_bounds <- attr(x, "include_bounds")
min_value <- attr(x, "min_value")
range_difference <- attr(x, "range_difference")
Expand Down Expand Up @@ -84,7 +86,7 @@ unnormalize.data.frame <- function(x,
}

for (i in select) {
var_attr <- which(grepl(paste0("^attr\\_", i, "\\."), names(grp_attr_dw)))
var_attr <- grep(paste0("^attr\\_", i, "\\."), names(grp_attr_dw))
attrs <- grp_attr_dw[var_attr]
x[[i]] <- unnormalize(x[[i]], verbose = verbose, grp_attr_dw = attrs)
}
Expand Down Expand Up @@ -147,5 +149,6 @@ unnormalize.grouped_df <- function(x,
}
# set back class, so data frame still works with dplyr
attributes(x) <- utils::modifyList(info, attributes(x))
class(x) <- c("grouped_df", class(x))
x
}
1 change: 1 addition & 0 deletions R/unstandardize.R
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ unstandardize.grouped_df <- function(x,
}
# set back class, so data frame still works with dplyr
attributes(x) <- utils::modifyList(info, attributes(x))
class(x) <- c("grouped_df", class(x))
x
}

Expand Down
31 changes: 18 additions & 13 deletions tests/testthat/test-standardize-data.R
Original file line number Diff line number Diff line change
Expand Up @@ -285,42 +285,47 @@ test_that("unstandardize: grouped data", {
skip_if_not_installed("poorman")

# 1 group, 1 standardized var
norm <- poorman::group_by(iris, Species)
norm <- standardize(norm, "Sepal.Length")
stand <- poorman::group_by(iris, Species)
stand <- standardize(stand, "Sepal.Length")
unstand <- unstandardize(stand, select = "Sepal.Length")
expect_identical(
poorman::ungroup(unstandardize(norm, select = "Sepal.Length")),
poorman::ungroup(unstand),
iris
)

expect_s3_class(unstand, "grouped_df")

# 2 groups, 1 standardized var
set.seed(123)
test <- iris
test$grp <- sample(c("A", "B"), nrow(test), replace = TRUE)
norm <- poorman::group_by(test, Species, grp)
norm <- standardize(norm, "Sepal.Length")
stand <- poorman::group_by(test, Species, grp)
stand <- standardize(stand, "Sepal.Length")
expect_identical(
poorman::ungroup(unstandardize(norm, select = "Sepal.Length")),
poorman::ungroup(unstandardize(stand, select = "Sepal.Length")),
test
)

# 2 groups, 2 standardized vars
set.seed(123)
test <- iris
test$grp <- sample(c("A", "B"), nrow(test), replace = TRUE)
norm <- poorman::group_by(test, Species, grp)
norm <- standardize(norm, c("Sepal.Length", "Petal.Length"))
stand <- poorman::group_by(test, Species, grp)
stand <- standardize(stand, c("Sepal.Length", "Petal.Length"))
expect_identical(
poorman::ungroup(unstandardize(norm, select = c("Sepal.Length", "Petal.Length"))),
poorman::ungroup(unstandardize(stand, select = c("Sepal.Length", "Petal.Length"))),
test
)

expect_s3_class(unstand, "grouped_df")

# can't recover attributes
norm <- poorman::group_by(iris, Species)
norm <- standardize(norm, "Sepal.Length")
attr(norm, "groups") <- NULL
stand <- poorman::group_by(iris, Species)
stand <- standardize(stand, "Sepal.Length")
attr(stand, "groups") <- NULL

expect_error(
unstandardize(norm, "Sepal.Length"),
unstandardize(stand, "Sepal.Length"),
regexp = "Couldn't retrieve the necessary information"
)
})
5 changes: 4 additions & 1 deletion tests/testthat/test-unnormalize.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,14 @@ test_that("unnormalize: grouped data", {
test$grp <- sample(c("A", "B"), nrow(test), replace = TRUE)
norm <- poorman::group_by(test, Species, grp)
norm <- normalize(norm, c("Sepal.Length", "Petal.Length"))
unnorm <- unnormalize(norm, c("Sepal.Length", "Petal.Length"))
expect_identical(
poorman::ungroup(unnormalize(norm, c("Sepal.Length", "Petal.Length"))),
poorman::ungroup(unnorm),
test
)

expect_s3_class(unnorm, "grouped_df")

# can't recover attributes
norm <- poorman::group_by(iris, Species)
norm <- normalize(norm, "Sepal.Length")
Expand Down

0 comments on commit 86478c2

Please sign in to comment.