From e78ae5c7571cfc594c7b8bcbbcdf786ef373fb88 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 14 Jun 2023 11:16:50 +0200 Subject: [PATCH 01/20] fix when overwriting existing variables in grouped_df (#434) --- DESCRIPTION | 2 +- R/data_modify.R | 7 ++++++- tests/testthat/test-data_modify.R | 15 +++++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index f3e9e0c0e..7d1bc8e8e 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: datawizard Title: Easy Data Wrangling and Statistical Transformations -Version: 0.7.1.11 +Version: 0.7.1.12 Authors@R: c( person("Indrajeet", "Patil", , "patilindrajeet.science@gmail.com", role = "aut", comment = c(ORCID = "0000-0003-1995-6531", Twitter = "@patilindrajeets")), diff --git a/R/data_modify.R b/R/data_modify.R index eb2873ac9..8a899059d 100644 --- a/R/data_modify.R +++ b/R/data_modify.R @@ -240,7 +240,12 @@ data_modify.grouped_df <- function(data, ...) { # create new variables as dummys, do for-loop works for (i in names(dots)) { - data[[i]] <- NA + # don't overwrite / fill existing variables with NA, + # e.g. if we have "data_modify(iris, Sepal.Length = normalize(Sepal.Length))" + # normalize() won't work when we fill with NA + if (!i %in% colnames(data)) { + data[[i]] <- NA + } } # create new variables per group diff --git a/tests/testthat/test-data_modify.R b/tests/testthat/test-data_modify.R index 4a712b2c7..b9f3d8fc8 100644 --- a/tests/testthat/test-data_modify.R +++ b/tests/testthat/test-data_modify.R @@ -467,3 +467,18 @@ test_that("data_modify works with character variables, and inside functions", { tolerance = 1e-3 ) }) + + +test_that("data_modify works with grouped df when overwriting existing variables", { + data(iris) + iris_grp <- data_group(iris, "Species") + out <- data_modify(iris_grp, Sepal.Length = normalize(Sepal.Length)) + expect_equal(head(out$Sepal.Length), c(0.53333, 0.4, 0.26667, 0.2, 0.46667, 0.73333), tolerance = 1e-3) + + out <- data_modify( + iris_grp, + Sepal.Length = normalize(Sepal.Length), + Sepal.Length2 = 2 * Sepal.Length + ) + expect_equal(head(out$Sepal.Length2), 2 * c(0.53333, 0.4, 0.26667, 0.2, 0.46667, 0.73333), tolerance = 1e-3) +}) From f4772413afaaa30ca126b4fd2f12f42fbdbac24a Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 14 Jun 2023 13:07:03 +0200 Subject: [PATCH 02/20] data_modify works with functions that return character vectors --- DESCRIPTION | 2 +- R/data_modify.R | 12 +++++++++++- tests/testthat/test-data_modify.R | 8 ++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 7d1bc8e8e..b1a68f5c3 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: datawizard Title: Easy Data Wrangling and Statistical Transformations -Version: 0.7.1.12 +Version: 0.7.1.13 Authors@R: c( person("Indrajeet", "Patil", , "patilindrajeet.science@gmail.com", role = "aut", comment = c(ORCID = "0000-0003-1995-6531", Twitter = "@patilindrajeets")), diff --git a/R/data_modify.R b/R/data_modify.R index 8a899059d..8ff1f3a75 100644 --- a/R/data_modify.R +++ b/R/data_modify.R @@ -149,7 +149,17 @@ data_modify.data.frame <- function(data, ...) { if (!is.character(symbol)) { eval_symbol <- .dynEval(symbol, ifnotfound = NULL) if (is.character(eval_symbol)) { - symbol <- str2lang(paste0(names(dots)[i], " = ", eval_symbol)) + symbol <- try(str2lang(paste0(names(dots)[i], " = ", eval_symbol)), silent = TRUE) + # we may have the edge-case of having a function that returns a character + # vector, like "new_var = sample(letters[1:3])". In this case, "eval_symbol" + # is of type character, but no symbol, thus str2lang() above creates a + # wrong pattern. We then take "eval_symbol" as character input. + if (inherits(symbol, "try-error")) { + symbol <- str2lang(paste0( + names(dots)[i], + " = c(", paste0("\"", eval_symbol, "\"", collapse = ","), ")" + )) + } } } diff --git a/tests/testthat/test-data_modify.R b/tests/testthat/test-data_modify.R index b9f3d8fc8..661993ca5 100644 --- a/tests/testthat/test-data_modify.R +++ b/tests/testthat/test-data_modify.R @@ -482,3 +482,11 @@ test_that("data_modify works with grouped df when overwriting existing variables ) expect_equal(head(out$Sepal.Length2), 2 * c(0.53333, 0.4, 0.26667, 0.2, 0.46667, 0.73333), tolerance = 1e-3) }) + + +test_that("data_modify works with functions that return character vectors", { + data(iris) + set.seed(123) + out <- data_modify(iris, grp = sample(letters[1:3], nrow(iris), TRUE)) + expect_identical(head(out$grp), c("c", "c", "c", "b", "c", "b")) +}) From 9b2e2b5d49b15dd73a73f3b4aadbc081bc91921b Mon Sep 17 00:00:00 2001 From: Etienne Bacher <52219252+etiennebacher@users.noreply.github.com> Date: Wed, 14 Jun 2023 17:00:26 +0200 Subject: [PATCH 03/20] tests: remove a `skip()` condition that is never reached because `datawizard` depends on R >= 3.6 --- tests/testthat/test-standardize_models.R | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/testthat/test-standardize_models.R b/tests/testthat/test-standardize_models.R index e569ffebf..4e1706ce5 100644 --- a/tests/testthat/test-standardize_models.R +++ b/tests/testthat/test-standardize_models.R @@ -204,8 +204,6 @@ test_that("variables evaluated in the environment", { w <- capture_warnings(standardize(m)) expect_true(any(grepl("mtcars$mpg", w, fixed = TRUE))) - - skip_if(packageVersion("base") == package_version(3.4)) ## Note: # No idea why this is suddenly not giving a warning on older R versions. m <- lm(mtcars$mpg ~ mtcars$cyl + mtcars$am, data = mtcars) From 01ba903d6dbd01f1966f72ec9e7384474a0469cf Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 16 Jun 2023 08:29:32 +0200 Subject: [PATCH 04/20] close #149 --- DESCRIPTION | 2 +- NEWS.md | 5 +++++ R/to_numeric.R | 2 +- tests/testthat/test-data_to_numeric.R | 2 ++ 4 files changed, 9 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index b1a68f5c3..2d01b9ff2 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: datawizard Title: Easy Data Wrangling and Statistical Transformations -Version: 0.7.1.13 +Version: 0.7.1.14 Authors@R: c( person("Indrajeet", "Patil", , "patilindrajeet.science@gmail.com", role = "aut", comment = c(ORCID = "0000-0003-1995-6531", Twitter = "@patilindrajeets")), diff --git a/NEWS.md b/NEWS.md index e08aba37a..507caf916 100644 --- a/NEWS.md +++ b/NEWS.md @@ -42,6 +42,11 @@ NEW FUNCTIONS * `data_modify()`, to create new variables, or modify or remove existing variables in a data frame. +MINOR CHANGES + +* `as.numeric()` for variables of type `Date`, `POSIXct` and `POSIXlt` now + includes the class name in the warning message. + BUG FIXES * `standardize_parameters()` now works when the package namespace is in the model diff --git a/R/to_numeric.R b/R/to_numeric.R index bfbbe526c..3d3f1c7a5 100644 --- a/R/to_numeric.R +++ b/R/to_numeric.R @@ -161,7 +161,7 @@ to_numeric.logical <- to_numeric.numeric to_numeric.Date <- function(x, verbose = TRUE, ...) { if (verbose) { insight::format_warning( - "Converting a date-time variable into numeric.", + paste0("Converting a date-time variable of class `", class(x)[1], "` into numeric."), "Please note that this conversion probably does not return meaningful results." ) } diff --git a/tests/testthat/test-data_to_numeric.R b/tests/testthat/test-data_to_numeric.R index a878697ac..d8eededb7 100644 --- a/tests/testthat/test-data_to_numeric.R +++ b/tests/testthat/test-data_to_numeric.R @@ -9,6 +9,8 @@ test_that("convert character to numeric", { test_that("convert character to numeric Date", { expect_warning(expect_identical(to_numeric(as.Date("2022-01-01")), 18993)) + expect_warning(expect_identical(to_numeric(as.POSIXct("2022-01-01")), 1640991600)) + expect_warning(expect_identical(to_numeric(as.POSIXlt("2022-01-01")), 1640991600)) }) test_that("convert character to numeric preserve levels", { From 01632ee07e2479b20101766f3087a3964c32ae91 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 16 Jun 2023 08:32:10 +0200 Subject: [PATCH 05/20] no need to document dataframe method --- R/data_modify.R | 1 - man/data_modify.Rd | 3 --- 2 files changed, 4 deletions(-) diff --git a/R/data_modify.R b/R/data_modify.R index 8ff1f3a75..9fa9a3b9a 100644 --- a/R/data_modify.R +++ b/R/data_modify.R @@ -101,7 +101,6 @@ data_modify.default <- function(data, ...) { insight::format_error("`data` must be a data frame.") } -#' @rdname data_modify #' @export data_modify.data.frame <- function(data, ...) { dots <- eval(substitute(alist(...))) diff --git a/man/data_modify.Rd b/man/data_modify.Rd index 270d29fcf..7f13a8c08 100644 --- a/man/data_modify.Rd +++ b/man/data_modify.Rd @@ -2,12 +2,9 @@ % Please edit documentation in R/data_modify.R \name{data_modify} \alias{data_modify} -\alias{data_modify.data.frame} \title{Create new variables in a data frame} \usage{ data_modify(data, ...) - -\method{data_modify}{data.frame}(data, ...) } \arguments{ \item{data}{A data frame} From ac0dcbc24168c96b48471bd114fbf5f5b23521e5 Mon Sep 17 00:00:00 2001 From: Etienne Bacher <52219252+etiennebacher@users.noreply.github.com> Date: Fri, 16 Jun 2023 08:43:45 +0200 Subject: [PATCH 06/20] fix news --- NEWS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 507caf916..faa915b08 100644 --- a/NEWS.md +++ b/NEWS.md @@ -44,7 +44,7 @@ NEW FUNCTIONS MINOR CHANGES -* `as.numeric()` for variables of type `Date`, `POSIXct` and `POSIXlt` now +* `to_numeric()` for variables of type `Date`, `POSIXct` and `POSIXlt` now includes the class name in the warning message. BUG FIXES From 9f75f5ff740d471090c438300e10310d03d377da Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 16 Jun 2023 09:28:18 +0200 Subject: [PATCH 07/20] relax test --- tests/testthat/test-data_to_numeric.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/testthat/test-data_to_numeric.R b/tests/testthat/test-data_to_numeric.R index d8eededb7..6873da14e 100644 --- a/tests/testthat/test-data_to_numeric.R +++ b/tests/testthat/test-data_to_numeric.R @@ -9,8 +9,8 @@ test_that("convert character to numeric", { test_that("convert character to numeric Date", { expect_warning(expect_identical(to_numeric(as.Date("2022-01-01")), 18993)) - expect_warning(expect_identical(to_numeric(as.POSIXct("2022-01-01")), 1640991600)) - expect_warning(expect_identical(to_numeric(as.POSIXlt("2022-01-01")), 1640991600)) + expect_warning(to_numeric(as.POSIXct("2022-01-01"))) + expect_warning(to_numeric(as.POSIXlt("2022-01-01"))) }) test_that("convert character to numeric preserve levels", { From f3616c4c65311e927d45c9874b64f706f4b71848 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 16 Jun 2023 09:31:05 +0200 Subject: [PATCH 08/20] maybe this works --- tests/testthat/test-data_to_numeric.R | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/testthat/test-data_to_numeric.R b/tests/testthat/test-data_to_numeric.R index 6873da14e..68eb8f0dc 100644 --- a/tests/testthat/test-data_to_numeric.R +++ b/tests/testthat/test-data_to_numeric.R @@ -8,9 +8,9 @@ test_that("convert character to numeric", { }) test_that("convert character to numeric Date", { - expect_warning(expect_identical(to_numeric(as.Date("2022-01-01")), 18993)) - expect_warning(to_numeric(as.POSIXct("2022-01-01"))) - expect_warning(to_numeric(as.POSIXlt("2022-01-01"))) + expect_warning(expect_identical(to_numeric(as.Date("2022-01-01")), as.numeric(as.Date("2022-01-01")))) + expect_warning(expect_identical(to_numeric(as.POSIXct("2022-01-01")), as.numeric(as.POSIXct("2022-01-01")))) + expect_warning(expect_identical(to_numeric(as.POSIXlt("2022-01-01")), as.numeric(as.POSIXlt("2022-01-01")))) }) test_that("convert character to numeric preserve levels", { From 904d029a4797ad30e6703e5fac94619fa9667f7e Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 16 Jun 2023 10:24:22 +0200 Subject: [PATCH 09/20] improve printing for transformed variable (#435) * improve printing for transformed variable Fixes #22 * add tests --- DESCRIPTION | 2 +- NAMESPACE | 1 + NEWS.md | 3 + R/center.R | 29 +++- tests/testthat/_snaps/print.dw_transformer.md | 128 ++++++++++++++++++ tests/testthat/test-print.dw_transformer.R | 7 + 6 files changed, 168 insertions(+), 2 deletions(-) create mode 100644 tests/testthat/_snaps/print.dw_transformer.md create mode 100644 tests/testthat/test-print.dw_transformer.R diff --git a/DESCRIPTION b/DESCRIPTION index 2d01b9ff2..eba115fe0 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: datawizard Title: Easy Data Wrangling and Statistical Transformations -Version: 0.7.1.14 +Version: 0.7.1.15 Authors@R: c( person("Indrajeet", "Patil", , "patilindrajeet.science@gmail.com", role = "aut", comment = c(ORCID = "0000-0003-1995-6531", Twitter = "@patilindrajeets")), diff --git a/NAMESPACE b/NAMESPACE index c2b4ecdb7..08ec43927 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -86,6 +86,7 @@ S3method(print,data_codebook) S3method(print,dw_data_peek) S3method(print,dw_data_tabulate) S3method(print,dw_data_tabulates) +S3method(print,dw_transformer) S3method(print,parameters_distribution) S3method(print,parameters_kurtosis) S3method(print,parameters_skewness) diff --git a/NEWS.md b/NEWS.md index faa915b08..03adf374e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -47,6 +47,9 @@ MINOR CHANGES * `to_numeric()` for variables of type `Date`, `POSIXct` and `POSIXlt` now includes the class name in the warning message. +* Added a `print()` method for `center()`, `standardize()`, `normalize()` and + `rescale()`. + BUG FIXES * `standardize_parameters()` now works when the package namespace is in the model diff --git a/R/center.R b/R/center.R index e8382078e..eac550de1 100644 --- a/R/center.R +++ b/R/center.R @@ -225,7 +225,6 @@ center.data.frame <- function(x, } - #' @export center.grouped_df <- function(x, select = NULL, @@ -276,3 +275,31 @@ center.grouped_df <- function(x, attributes(args$x) <- args$info args$x } + + +# methods ------------------------- + +#' @export +print.dw_transformer <- function(x, ...) { + print(as.vector(x), ...) + vector_info <- NULL + if (!is.null(attributes(x)$scale)) { + # attributes for center() / standardize() + vector_info <- sprintf( + "(center: %.2g, scale = %.2g)\n", + attributes(x)$center, + attributes(x)$scale + ) + } else if (!is.null(attributes(x)$range_difference)) { + # attributes for normalize() / rescale() + vector_info <- sprintf( + "(original range = %.2g to %.2g)\n", + attributes(x)$min_value, + attributes(x)$min_value + attributes(x)$range_difference + ) + } + if (!is.null(vector_info)) { + insight::print_color(vector_info, color = "grey") + } + invisible(x) +} diff --git a/tests/testthat/_snaps/print.dw_transformer.md b/tests/testthat/_snaps/print.dw_transformer.md new file mode 100644 index 000000000..ecfcf6347 --- /dev/null +++ b/tests/testthat/_snaps/print.dw_transformer.md @@ -0,0 +1,128 @@ +# print.dw_transformer + + Code + rescale(iris$Sepal.Length) + Output + [1] 22.222222 16.666667 11.111111 8.333333 19.444444 30.555556 + [7] 8.333333 19.444444 2.777778 16.666667 30.555556 13.888889 + [13] 13.888889 0.000000 41.666667 38.888889 30.555556 22.222222 + [19] 38.888889 22.222222 30.555556 22.222222 8.333333 22.222222 + [25] 13.888889 19.444444 19.444444 25.000000 25.000000 11.111111 + [31] 13.888889 30.555556 25.000000 33.333333 16.666667 19.444444 + [37] 33.333333 16.666667 2.777778 22.222222 19.444444 5.555556 + [43] 2.777778 19.444444 22.222222 13.888889 22.222222 8.333333 + [49] 27.777778 19.444444 75.000000 58.333333 72.222222 33.333333 + [55] 61.111111 38.888889 55.555556 16.666667 63.888889 25.000000 + [61] 19.444444 44.444444 47.222222 50.000000 36.111111 66.666667 + [67] 36.111111 41.666667 52.777778 36.111111 44.444444 50.000000 + [73] 55.555556 50.000000 58.333333 63.888889 69.444444 66.666667 + [79] 47.222222 38.888889 33.333333 33.333333 41.666667 47.222222 + [85] 30.555556 47.222222 66.666667 55.555556 36.111111 33.333333 + [91] 33.333333 50.000000 41.666667 19.444444 36.111111 38.888889 + [97] 38.888889 52.777778 22.222222 38.888889 55.555556 41.666667 + [103] 77.777778 55.555556 61.111111 91.666667 16.666667 83.333333 + [109] 66.666667 80.555556 61.111111 58.333333 69.444444 38.888889 + [115] 41.666667 58.333333 61.111111 94.444444 94.444444 47.222222 + [121] 72.222222 36.111111 94.444444 55.555556 66.666667 80.555556 + [127] 52.777778 50.000000 58.333333 80.555556 86.111111 100.000000 + [133] 58.333333 55.555556 50.000000 94.444444 55.555556 58.333333 + [139] 47.222222 72.222222 66.666667 72.222222 41.666667 69.444444 + [145] 66.666667 66.666667 55.555556 61.111111 52.777778 44.444444 + (original range = 4.3 to 7.9) + +--- + + Code + normalize(iris$Sepal.Length) + Output + [1] 0.22222222 0.16666667 0.11111111 0.08333333 0.19444444 0.30555556 + [7] 0.08333333 0.19444444 0.02777778 0.16666667 0.30555556 0.13888889 + [13] 0.13888889 0.00000000 0.41666667 0.38888889 0.30555556 0.22222222 + [19] 0.38888889 0.22222222 0.30555556 0.22222222 0.08333333 0.22222222 + [25] 0.13888889 0.19444444 0.19444444 0.25000000 0.25000000 0.11111111 + [31] 0.13888889 0.30555556 0.25000000 0.33333333 0.16666667 0.19444444 + [37] 0.33333333 0.16666667 0.02777778 0.22222222 0.19444444 0.05555556 + [43] 0.02777778 0.19444444 0.22222222 0.13888889 0.22222222 0.08333333 + [49] 0.27777778 0.19444444 0.75000000 0.58333333 0.72222222 0.33333333 + [55] 0.61111111 0.38888889 0.55555556 0.16666667 0.63888889 0.25000000 + [61] 0.19444444 0.44444444 0.47222222 0.50000000 0.36111111 0.66666667 + [67] 0.36111111 0.41666667 0.52777778 0.36111111 0.44444444 0.50000000 + [73] 0.55555556 0.50000000 0.58333333 0.63888889 0.69444444 0.66666667 + [79] 0.47222222 0.38888889 0.33333333 0.33333333 0.41666667 0.47222222 + [85] 0.30555556 0.47222222 0.66666667 0.55555556 0.36111111 0.33333333 + [91] 0.33333333 0.50000000 0.41666667 0.19444444 0.36111111 0.38888889 + [97] 0.38888889 0.52777778 0.22222222 0.38888889 0.55555556 0.41666667 + [103] 0.77777778 0.55555556 0.61111111 0.91666667 0.16666667 0.83333333 + [109] 0.66666667 0.80555556 0.61111111 0.58333333 0.69444444 0.38888889 + [115] 0.41666667 0.58333333 0.61111111 0.94444444 0.94444444 0.47222222 + [121] 0.72222222 0.36111111 0.94444444 0.55555556 0.66666667 0.80555556 + [127] 0.52777778 0.50000000 0.58333333 0.80555556 0.86111111 1.00000000 + [133] 0.58333333 0.55555556 0.50000000 0.94444444 0.55555556 0.58333333 + [139] 0.47222222 0.72222222 0.66666667 0.72222222 0.41666667 0.69444444 + [145] 0.66666667 0.66666667 0.55555556 0.61111111 0.52777778 0.44444444 + (original range = 4.3 to 7.9) + +--- + + Code + center(iris$Sepal.Length) + Output + [1] -0.74333333 -0.94333333 -1.14333333 -1.24333333 -0.84333333 -0.44333333 + [7] -1.24333333 -0.84333333 -1.44333333 -0.94333333 -0.44333333 -1.04333333 + [13] -1.04333333 -1.54333333 -0.04333333 -0.14333333 -0.44333333 -0.74333333 + [19] -0.14333333 -0.74333333 -0.44333333 -0.74333333 -1.24333333 -0.74333333 + [25] -1.04333333 -0.84333333 -0.84333333 -0.64333333 -0.64333333 -1.14333333 + [31] -1.04333333 -0.44333333 -0.64333333 -0.34333333 -0.94333333 -0.84333333 + [37] -0.34333333 -0.94333333 -1.44333333 -0.74333333 -0.84333333 -1.34333333 + [43] -1.44333333 -0.84333333 -0.74333333 -1.04333333 -0.74333333 -1.24333333 + [49] -0.54333333 -0.84333333 1.15666667 0.55666667 1.05666667 -0.34333333 + [55] 0.65666667 -0.14333333 0.45666667 -0.94333333 0.75666667 -0.64333333 + [61] -0.84333333 0.05666667 0.15666667 0.25666667 -0.24333333 0.85666667 + [67] -0.24333333 -0.04333333 0.35666667 -0.24333333 0.05666667 0.25666667 + [73] 0.45666667 0.25666667 0.55666667 0.75666667 0.95666667 0.85666667 + [79] 0.15666667 -0.14333333 -0.34333333 -0.34333333 -0.04333333 0.15666667 + [85] -0.44333333 0.15666667 0.85666667 0.45666667 -0.24333333 -0.34333333 + [91] -0.34333333 0.25666667 -0.04333333 -0.84333333 -0.24333333 -0.14333333 + [97] -0.14333333 0.35666667 -0.74333333 -0.14333333 0.45666667 -0.04333333 + [103] 1.25666667 0.45666667 0.65666667 1.75666667 -0.94333333 1.45666667 + [109] 0.85666667 1.35666667 0.65666667 0.55666667 0.95666667 -0.14333333 + [115] -0.04333333 0.55666667 0.65666667 1.85666667 1.85666667 0.15666667 + [121] 1.05666667 -0.24333333 1.85666667 0.45666667 0.85666667 1.35666667 + [127] 0.35666667 0.25666667 0.55666667 1.35666667 1.55666667 2.05666667 + [133] 0.55666667 0.45666667 0.25666667 1.85666667 0.45666667 0.55666667 + [139] 0.15666667 1.05666667 0.85666667 1.05666667 -0.04333333 0.95666667 + [145] 0.85666667 0.85666667 0.45666667 0.65666667 0.35666667 0.05666667 + (center: 5.8, scale = 1) + +--- + + Code + standardize(iris$Sepal.Length) + Output + [1] -0.89767388 -1.13920048 -1.38072709 -1.50149039 -1.01843718 -0.53538397 + [7] -1.50149039 -1.01843718 -1.74301699 -1.13920048 -0.53538397 -1.25996379 + [13] -1.25996379 -1.86378030 -0.05233076 -0.17309407 -0.53538397 -0.89767388 + [19] -0.17309407 -0.89767388 -0.53538397 -0.89767388 -1.50149039 -0.89767388 + [25] -1.25996379 -1.01843718 -1.01843718 -0.77691058 -0.77691058 -1.38072709 + [31] -1.25996379 -0.53538397 -0.77691058 -0.41462067 -1.13920048 -1.01843718 + [37] -0.41462067 -1.13920048 -1.74301699 -0.89767388 -1.01843718 -1.62225369 + [43] -1.74301699 -1.01843718 -0.89767388 -1.25996379 -0.89767388 -1.50149039 + [49] -0.65614727 -1.01843718 1.39682886 0.67224905 1.27606556 -0.41462067 + [55] 0.79301235 -0.17309407 0.55148575 -1.13920048 0.91377565 -0.77691058 + [61] -1.01843718 0.06843254 0.18919584 0.30995914 -0.29385737 1.03453895 + [67] -0.29385737 -0.05233076 0.43072244 -0.29385737 0.06843254 0.30995914 + [73] 0.55148575 0.30995914 0.67224905 0.91377565 1.15530226 1.03453895 + [79] 0.18919584 -0.17309407 -0.41462067 -0.41462067 -0.05233076 0.18919584 + [85] -0.53538397 0.18919584 1.03453895 0.55148575 -0.29385737 -0.41462067 + [91] -0.41462067 0.30995914 -0.05233076 -1.01843718 -0.29385737 -0.17309407 + [97] -0.17309407 0.43072244 -0.89767388 -0.17309407 0.55148575 -0.05233076 + [103] 1.51759216 0.55148575 0.79301235 2.12140867 -1.13920048 1.75911877 + [109] 1.03453895 1.63835547 0.79301235 0.67224905 1.15530226 -0.17309407 + [115] -0.05233076 0.67224905 0.79301235 2.24217198 2.24217198 0.18919584 + [121] 1.27606556 -0.29385737 2.24217198 0.55148575 1.03453895 1.63835547 + [127] 0.43072244 0.30995914 0.67224905 1.63835547 1.87988207 2.48369858 + [133] 0.67224905 0.55148575 0.30995914 2.24217198 0.55148575 0.67224905 + [139] 0.18919584 1.27606556 1.03453895 1.27606556 -0.05233076 1.15530226 + [145] 1.03453895 1.03453895 0.55148575 0.79301235 0.43072244 0.06843254 + (center: 5.8, scale = 0.83) + diff --git a/tests/testthat/test-print.dw_transformer.R b/tests/testthat/test-print.dw_transformer.R new file mode 100644 index 000000000..0dbb1dc8d --- /dev/null +++ b/tests/testthat/test-print.dw_transformer.R @@ -0,0 +1,7 @@ +test_that("print.dw_transformer", { + data(iris) + expect_snapshot(rescale(iris$Sepal.Length)) + expect_snapshot(normalize(iris$Sepal.Length)) + expect_snapshot(center(iris$Sepal.Length)) + expect_snapshot(standardize(iris$Sepal.Length)) +}) From 7cb3d61f1daa8c82f51593de0066124206e4b001 Mon Sep 17 00:00:00 2001 From: Etienne Bacher <52219252+etiennebacher@users.noreply.github.com> Date: Sat, 17 Jun 2023 00:00:55 +0200 Subject: [PATCH 10/20] Release 0.8.0 (#436) * update cran comments * bump version number [skip ci] --------- Co-authored-by: Daniel --- DESCRIPTION | 2 +- NEWS.md | 2 +- cran-comments.md | 5 +++++ 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index eba115fe0..fa81df8b6 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: datawizard Title: Easy Data Wrangling and Statistical Transformations -Version: 0.7.1.15 +Version: 0.8.0 Authors@R: c( person("Indrajeet", "Patil", , "patilindrajeet.science@gmail.com", role = "aut", comment = c(ORCID = "0000-0003-1995-6531", Twitter = "@patilindrajeets")), diff --git a/NEWS.md b/NEWS.md index 03adf374e..3b15bb71e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,4 @@ -# datawizard (devel) +# datawizard 0.8.0 BREAKING CHANGES diff --git a/cran-comments.md b/cran-comments.md index b883aa2b6..6f845d45c 100644 --- a/cran-comments.md +++ b/cran-comments.md @@ -8,3 +8,8 @@ We checked 16 reverse dependencies, comparing R CMD check results across CRAN an * We saw 0 new problems * We failed to check 0 packages + +## Other comments + +This release fixes the issue with `package_version()` reported by Kurt Hornik +on June 14th. From 3cdc3fa44585a446101e6102bbdd297b83a7cc73 Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 20 Jun 2023 22:04:00 +0200 Subject: [PATCH 11/20] fix data_write for new haven (#437) * fix data_write for new haven * add test * desc, news * comment * styler * fix test --- DESCRIPTION | 2 +- NEWS.md | 7 +++++++ R/data_write.r | 18 +++++++++++++++--- tests/testthat/test-data_write.R | 23 +++++++++++++++++++++++ 4 files changed, 46 insertions(+), 4 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index fa81df8b6..6562deae0 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: datawizard Title: Easy Data Wrangling and Statistical Transformations -Version: 0.8.0 +Version: 0.8.0.1 Authors@R: c( person("Indrajeet", "Patil", , "patilindrajeet.science@gmail.com", role = "aut", comment = c(ORCID = "0000-0003-1995-6531", Twitter = "@patilindrajeets")), diff --git a/NEWS.md b/NEWS.md index 3b15bb71e..3622c1738 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,10 @@ +# datawizard (devel) + +BUG FIXES + +* Fixed issues in `data_write()` when writing labelled data into SPSS format + and vectors were of different type as value labels. + # datawizard 0.8.0 BREAKING CHANGES diff --git a/R/data_write.r b/R/data_write.r index 8a868d7ae..b8d710d2e 100644 --- a/R/data_write.r +++ b/R/data_write.r @@ -127,18 +127,30 @@ data_write <- function(data, insight::format_alert("Preparing data file: converting variable types.") } x[] <- lapply(x, function(i) { + # make sure we have labelled class for labelled data value_labels <- attr(i, "labels", exact = TRUE) variable_label <- attr(i, "label", exact = TRUE) + # factor requires special preparation to save levels as labels + # haven:::vec_cast_named requires "x" and "labels" to be of same type if (is.factor(i)) { - # factor requires special preparation to save levels as labels haven::labelled( x = as.numeric(i), labels = stats::setNames(seq_along(levels(i)), levels(i)), label = variable_label ) } else if (!is.null(value_labels) || !is.null(variable_label)) { - # make sure we have labelled class for labelled data - haven::labelled(x = i, labels = value_labels, label = variable_label) + # character requires special preparation to save value labels + # haven:::vec_cast_named requires "x" and "labels" to be of same type + if (is.character(i)) { + haven::labelled( + x = i, + labels = stats::setNames(as.character(value_labels), names(value_labels)), + label = variable_label + ) + } else { + # this should work for the remaining types... + haven::labelled(x = i, labels = value_labels, label = variable_label) + } } else { # non labelled data can be saved "as is" i diff --git a/tests/testthat/test-data_write.R b/tests/testthat/test-data_write.R index 42cc63e9f..a51931bd7 100644 --- a/tests/testthat/test-data_write.R +++ b/tests/testthat/test-data_write.R @@ -30,6 +30,29 @@ test_that("data_write, SPSS", { }) +tmp <- tempfile(fileext = ".sav") +on.exit(unlink(tmp)) + +test_that("data_write, SPSS, mixed types of labelled vectors", { + d <- data.frame( + a = 1:3, + b = letters[1:3], + c = factor(letters[1:3]), + d = as.Date(c("2022-01-01", "2022-02-01", "2022-03-01")), + e = c(TRUE, FALSE, FALSE), + stringsAsFactors = FALSE + ) + + # Date and Logical cannot be labelled + d$a <- assign_labels(d$a, variable = "First", values = c("one", "two", "three")) + d$b <- assign_labels(d$b, variable = "Second", values = c("A", "B", "C")) + d$c <- assign_labels(d$c, variable = "Third", values = c("ey", "bee", "see")) + + # expect message, but no error + expect_message(data_write(d, "test.sav"), regex = "Preparing") +}) + + # Stata ------------------------------------- From 0be859cb4fb9dc9903ebce6411175bd6ff8a08d9 Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 27 Jun 2023 10:58:39 +0200 Subject: [PATCH 12/20] recode_into can skip overwriting former recodes (#438) * recode_into can skip overwriting former recodes * news., desc * fix test * fix, add test * remove commented code --- DESCRIPTION | 2 +- NEWS.md | 5 ++++ R/recode_into.r | 27 +++++++++++++---- man/recode_into.Rd | 8 ++++- tests/testthat/test-recode_into.R | 49 +++++++++++++++++++++++++++++++ 5 files changed, 83 insertions(+), 8 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 6562deae0..93425bdbb 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: datawizard Title: Easy Data Wrangling and Statistical Transformations -Version: 0.8.0.1 +Version: 0.8.0.2 Authors@R: c( person("Indrajeet", "Patil", , "patilindrajeet.science@gmail.com", role = "aut", comment = c(ORCID = "0000-0003-1995-6531", Twitter = "@patilindrajeets")), diff --git a/NEWS.md b/NEWS.md index 3622c1738..9b901a630 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,10 @@ # datawizard (devel) +CHANGES + +* `recode_into()` gains an `overwrite` argument to skip overwriting already + recoded cases when multiple recode patterns apply to the same case. + BUG FIXES * Fixed issues in `data_write()` when writing labelled data into SPSS format diff --git a/R/recode_into.r b/R/recode_into.r index 9bfeb27a4..add3f27f3 100644 --- a/R/recode_into.r +++ b/R/recode_into.r @@ -15,6 +15,11 @@ #' @param default Indicates the default value that is chosen when no match in #' the formulas in `...` is found. If not provided, `NA` is used as default #' value. +#' @param overwrite Logical, if `TRUE` (default) and more than one recode pattern +#' apply to the same case, already recoded values will be overwritten by subsequent +#' recode patterns. If `FALSE`, former recoded cases will not be altered by later +#' recode patterns that would apply to those cases again. A warning message is +#' printed to alert such situations and to avoid unintentional recodings. #' @param verbose Toggle warnings. #' #' @return A vector with recoded values. @@ -49,7 +54,7 @@ #' default = 0 #' ) #' @export -recode_into <- function(..., data = NULL, default = NA, verbose = TRUE) { +recode_into <- function(..., data = NULL, default = NA, overwrite = TRUE, verbose = TRUE) { dots <- list(...) # get length of vector, so we know the length of the output vector @@ -118,14 +123,24 @@ recode_into <- function(..., data = NULL, default = NA, verbose = TRUE) { already_exists <- out[index] != default } if (any(already_exists) && verbose) { - insight::format_warning( - paste( + if (overwrite) { + msg <- paste( "Several recode patterns apply to the same cases.", "Some of the already recoded cases will be overwritten with new values again", sprintf("(e.g. pattern %i overwrites the former recode of case %i).", i, which(already_exists)[1]) - ), - "Please check if this is intentional!" - ) + ) + } else { + msg <- paste( + "Several recode patterns apply to the same cases.", + "Some of the already recoded cases will not be altered by later recode patterns.", + sprintf("(e.g. pattern %i also matches the former recode of case %i).", i, which(already_exists)[1]) + ) + } + insight::format_warning(msg, "Please check if this is intentional!") + } + # if user doesn't want to overwrite, remove already recoded indices + if (!overwrite) { + index[which(index)[already_exists]] <- FALSE } out[index] <- value } diff --git a/man/recode_into.Rd b/man/recode_into.Rd index 8c197d391..2f8f3203a 100644 --- a/man/recode_into.Rd +++ b/man/recode_into.Rd @@ -4,7 +4,7 @@ \alias{recode_into} \title{Recode values from one or more variables into a new variable} \usage{ -recode_into(..., data = NULL, default = NA, verbose = TRUE) +recode_into(..., data = NULL, default = NA, overwrite = TRUE, verbose = TRUE) } \arguments{ \item{...}{A sequence of two-sided formulas, where the left hand side (LHS) @@ -19,6 +19,12 @@ the data name multiple times in \code{...}. See 'Examples'.} the formulas in \code{...} is found. If not provided, \code{NA} is used as default value.} +\item{overwrite}{Logical, if \code{TRUE} (default) and more than one recode pattern +apply to the same case, already recoded values will be overwritten by subsequent +recode patterns. If \code{FALSE}, former recoded cases will not be altered by later +recode patterns that would apply to those cases again. A warning message is +printed to alert such situations and to avoid unintentional recodings.} + \item{verbose}{Toggle warnings.} } \value{ diff --git a/tests/testthat/test-recode_into.R b/tests/testthat/test-recode_into.R index 13b239498..aaed73b34 100644 --- a/tests/testthat/test-recode_into.R +++ b/tests/testthat/test-recode_into.R @@ -8,6 +8,55 @@ test_that("recode_into", { expect_identical(out, c("c", "c", "b", "b", "b", "a", "a", "a", "a", "a")) }) +test_that("recode_into, overwrite", { + x <- 1:30 + expect_warning( + recode_into( + x > 1 ~ "a", + x > 10 & x <= 15 ~ "b", + default = "c", + overwrite = TRUE + ), + regex = "overwritten" + ) + # validate results + x <- 1:10 + expect_silent({ + out <- recode_into( + x >= 3 & x <= 7 ~ 1, + x > 5 ~ 2, + default = 0, + verbose = FALSE + ) + }) + expect_identical(out, c(0, 0, 1, 1, 1, 2, 2, 2, 2, 2)) + + x <- 1:10 + expect_silent({ + out <- recode_into( + x >= 3 & x <= 7 ~ 1, + x > 5 ~ 2, + default = 0, + overwrite = FALSE, + verbose = FALSE + ) + }) + expect_identical(out, c(0, 0, 1, 1, 1, 1, 1, 2, 2, 2)) +}) + +test_that("recode_into, don't overwrite", { + x <- 1:30 + expect_warning( + recode_into( + x > 1 ~ "a", + x > 10 & x <= 15 ~ "b", + default = "c", + overwrite = FALSE + ), + regex = "altered" + ) +}) + test_that("recode_into, check mixed types", { x <- 1:10 expect_error( From 17d77665b171f9b9077447b8313b96cf016408ac Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 27 Jun 2023 11:04:03 +0200 Subject: [PATCH 13/20] docs --- R/recode_into.r | 19 +++++++++++++++++++ man/recode_into.Rd | 18 ++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/R/recode_into.r b/R/recode_into.r index add3f27f3..bb47131c4 100644 --- a/R/recode_into.r +++ b/R/recode_into.r @@ -32,6 +32,25 @@ #' default = "c" #' ) #' +#' x <- 1:10 +#' # default behaviour: second recode pattern "x > 5" overwrites +#' # some of the formerly recoded cases from pattern "x >= 3 & x <= 7" +#' recode_into( +#' x >= 3 & x <= 7 ~ 1, +#' x > 5 ~ 2, +#' default = 0, +#' verbose = FALSE +#' ) +#' +#' # setting "overwrite = FALSE" will not alter formerly recoded cases +#' recode_into( +#' x >= 3 & x <= 7 ~ 1, +#' x > 5 ~ 2, +#' default = 0, +#' overwrote = FALSE, +#' verbose = FALSE +#' ) + #' set.seed(123) #' d <- data.frame( #' x = sample(1:5, 30, TRUE), diff --git a/man/recode_into.Rd b/man/recode_into.Rd index 2f8f3203a..306b2f8d7 100644 --- a/man/recode_into.Rd +++ b/man/recode_into.Rd @@ -43,6 +43,24 @@ recode_into( default = "c" ) +x <- 1:10 +# default behaviour: second recode pattern "x > 5" overwrites +# some of the formerly recoded cases from pattern "x >= 3 & x <= 7" +recode_into( + x >= 3 & x <= 7 ~ 1, + x > 5 ~ 2, + default = 0, + verbose = FALSE +) + +# setting "overwrite = FALSE" will not alter formerly recoded cases +recode_into( + x >= 3 & x <= 7 ~ 1, + x > 5 ~ 2, + default = 0, + overwrote = FALSE, + verbose = FALSE +) set.seed(123) d <- data.frame( x = sample(1:5, 30, TRUE), From df85e1d70c163f56575d783390ff7e6de4f21ed1 Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 27 Jun 2023 11:04:58 +0200 Subject: [PATCH 14/20] docs (add example) --- R/recode_into.r | 4 ++-- man/recode_into.Rd | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/R/recode_into.r b/R/recode_into.r index bb47131c4..828b2df5c 100644 --- a/R/recode_into.r +++ b/R/recode_into.r @@ -47,10 +47,10 @@ #' x >= 3 & x <= 7 ~ 1, #' x > 5 ~ 2, #' default = 0, -#' overwrote = FALSE, +#' overwrite = FALSE, #' verbose = FALSE #' ) - +#' #' set.seed(123) #' d <- data.frame( #' x = sample(1:5, 30, TRUE), diff --git a/man/recode_into.Rd b/man/recode_into.Rd index 306b2f8d7..064d72f6c 100644 --- a/man/recode_into.Rd +++ b/man/recode_into.Rd @@ -58,9 +58,10 @@ recode_into( x >= 3 & x <= 7 ~ 1, x > 5 ~ 2, default = 0, - overwrote = FALSE, + overwrite = FALSE, verbose = FALSE ) + set.seed(123) d <- data.frame( x = sample(1:5, 30, TRUE), From a14b606ecb57bd83bb830a0d70552779c3044b29 Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 27 Jun 2023 13:15:10 +0200 Subject: [PATCH 15/20] fix wrong case number in warning (#439) --- DESCRIPTION | 2 +- NEWS.md | 3 +++ R/recode_into.r | 9 ++++++--- tests/testthat/test-recode_into.R | 17 +++++++++++++++++ 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 93425bdbb..0f8e29314 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: datawizard Title: Easy Data Wrangling and Statistical Transformations -Version: 0.8.0.2 +Version: 0.8.0.3 Authors@R: c( person("Indrajeet", "Patil", , "patilindrajeet.science@gmail.com", role = "aut", comment = c(ORCID = "0000-0003-1995-6531", Twitter = "@patilindrajeets")), diff --git a/NEWS.md b/NEWS.md index 9b901a630..63a94bbaf 100644 --- a/NEWS.md +++ b/NEWS.md @@ -10,6 +10,9 @@ BUG FIXES * Fixed issues in `data_write()` when writing labelled data into SPSS format and vectors were of different type as value labels. +* Fixed issue in `recode_into()` with probably wrong case number printed in the + warning when several recode patterns match to one case. + # datawizard 0.8.0 BREAKING CHANGES diff --git a/R/recode_into.r b/R/recode_into.r index 828b2df5c..8a382fbe1 100644 --- a/R/recode_into.r +++ b/R/recode_into.r @@ -141,25 +141,28 @@ recode_into <- function(..., data = NULL, default = NA, overwrite = TRUE, verbos } else { already_exists <- out[index] != default } + # save indices of overwritten cases + overwritten_cases <- which(index)[already_exists] + # tell user... if (any(already_exists) && verbose) { if (overwrite) { msg <- paste( "Several recode patterns apply to the same cases.", "Some of the already recoded cases will be overwritten with new values again", - sprintf("(e.g. pattern %i overwrites the former recode of case %i).", i, which(already_exists)[1]) + sprintf("(e.g. pattern %i overwrites the former recode of case %i).", i, overwritten_cases[1]) ) } else { msg <- paste( "Several recode patterns apply to the same cases.", "Some of the already recoded cases will not be altered by later recode patterns.", - sprintf("(e.g. pattern %i also matches the former recode of case %i).", i, which(already_exists)[1]) + sprintf("(e.g. pattern %i also matches the former recode of case %i).", i, overwritten_cases[1]) ) } insight::format_warning(msg, "Please check if this is intentional!") } # if user doesn't want to overwrite, remove already recoded indices if (!overwrite) { - index[which(index)[already_exists]] <- FALSE + index[overwritten_cases] <- FALSE } out[index] <- value } diff --git a/tests/testthat/test-recode_into.R b/tests/testthat/test-recode_into.R index aaed73b34..ab5c7908b 100644 --- a/tests/testthat/test-recode_into.R +++ b/tests/testthat/test-recode_into.R @@ -30,6 +30,14 @@ test_that("recode_into, overwrite", { ) }) expect_identical(out, c(0, 0, 1, 1, 1, 2, 2, 2, 2, 2)) + expect_warning( + recode_into( + x >= 3 & x <= 7 ~ 1, + x > 5 ~ 2, + default = 0 + ), + regex = "case 6" + ) x <- 1:10 expect_silent({ @@ -42,6 +50,15 @@ test_that("recode_into, overwrite", { ) }) expect_identical(out, c(0, 0, 1, 1, 1, 1, 1, 2, 2, 2)) + expect_warning( + recode_into( + x >= 3 & x <= 7 ~ 1, + x > 5 ~ 2, + default = 0, + overwrite = FALSE + ), + regex = "case 6" + ) }) test_that("recode_into, don't overwrite", { From 3ff2209ba645c7bf1e6614488f2ec54d2df7467c Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 28 Jun 2023 14:21:39 +0200 Subject: [PATCH 16/20] fix for functions (#440) * fix for functions * comment * this is intentional * styler * disable styler, is intentional --- DESCRIPTION | 2 +- NEWS.md | 4 +++ R/data_match.R | 56 +++++++++++++++++++------------- tests/testthat/test-data_match.R | 25 ++++++++++++++ 4 files changed, 63 insertions(+), 24 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 0f8e29314..6a0264ee0 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: datawizard Title: Easy Data Wrangling and Statistical Transformations -Version: 0.8.0.3 +Version: 0.8.0.4 Authors@R: c( person("Indrajeet", "Patil", , "patilindrajeet.science@gmail.com", role = "aut", comment = c(ORCID = "0000-0003-1995-6531", Twitter = "@patilindrajeets")), diff --git a/NEWS.md b/NEWS.md index 63a94bbaf..c3aee3b24 100644 --- a/NEWS.md +++ b/NEWS.md @@ -13,6 +13,10 @@ BUG FIXES * Fixed issue in `recode_into()` with probably wrong case number printed in the warning when several recode patterns match to one case. +* Fixed issue in `data_filter()` where functions containing a `=` (e.g. when + naming arguments, like `grepl(pattern, x = a)`) were mistakenly seen as + faulty syntax. + # datawizard 0.8.0 BREAKING CHANGES diff --git a/R/data_match.R b/R/data_match.R index 537191409..6b931c292 100644 --- a/R/data_match.R +++ b/R/data_match.R @@ -311,29 +311,39 @@ data_filter.grouped_df <- function(x, ...) { tmp <- gsub(">=", "", tmp, fixed = TRUE) tmp <- gsub("!=", "", tmp, fixed = TRUE) - # Give more informative message to users - # about possible misspelled comparisons / logical conditions - # check if "=" instead of "==" was used? - if (any(grepl("=", tmp, fixed = TRUE))) { - insight::format_error( - "Filtering did not work. Please check if you need `==` (instead of `=`) for comparison." - ) - } - # check if "&&" etc instead of "&" was used? - logical_operator <- NULL - if (any(grepl("&&", .fcondition, fixed = TRUE))) { - logical_operator <- "&&" - } - if (any(grepl("||", .fcondition, fixed = TRUE))) { - logical_operator <- "||" - } - if (!is.null(logical_operator)) { - insight::format_error( - paste0( - "Filtering did not work. Please check if you need `", - substr(logical_operator, 0, 1), - "` (instead of `", logical_operator, "`) as logical operator." + # We want to check whether user used a "=" in the filter syntax. This + # typically indicates that the comparison "==" is probably wrong by using + # a "=" instead of `"=="`. However, if a function was provided, we indeed + # may have "=", e.g. if the pattern was + # `data_filter(out, grep("pattern", x = value))`. We thus first check if we + # can identify a function call, and only continue checking for wrong syntax + # when we have not identified a function. + + if (!is.function(try(get(gsub("^(.*?)\\((.*)", "\\1", tmp)), silent = TRUE))) { + # Give more informative message to users + # about possible misspelled comparisons / logical conditions + # check if "=" instead of "==" was used? + if (any(grepl("=", tmp, fixed = TRUE))) { + insight::format_error( + "Filtering did not work. Please check if you need `==` (instead of `=`) for comparison." ) - ) + } + # check if "&&" etc instead of "&" was used? + logical_operator <- NULL + if (any(grepl("&&", .fcondition, fixed = TRUE))) { + logical_operator <- "&&" + } + if (any(grepl("||", .fcondition, fixed = TRUE))) { + logical_operator <- "||" + } + if (!is.null(logical_operator)) { + insight::format_error( + paste0( + "Filtering did not work. Please check if you need `", + substr(logical_operator, 0, 1), + "` (instead of `", logical_operator, "`) as logical operator." + ) + ) + } } } diff --git a/tests/testthat/test-data_match.R b/tests/testthat/test-data_match.R index d803cdd44..75991b4b2 100644 --- a/tests/testthat/test-data_match.R +++ b/tests/testthat/test-data_match.R @@ -320,3 +320,28 @@ test_that("data_filter with groups, different ways of dots", { expect_identical(out1, out2) expect_identical(out1, out3) }) + + +test_that("data_filter, slicing works with functions", { + d <- data.frame( + a = c("aa", "a1", "bb", "b1", "cc", "c1"), + b = 1:6, + stringsAsFactors = FALSE + ) + + rows <- grep("^[A-Za-z][0-9]$", x = d$a) + out1 <- data_filter(d, rows) + out2 <- data_filter(d, grep("^[A-Za-z][0-9]$", x = d$a)) + + expect_identical(out1, out2) + + out3 <- data_filter(iris, (Sepal.Width == 3.0) & (Species == "setosa")) + expect_identical(nrow(out3), 6L) + + # styler: off + expect_error( + data_filter(iris, (Sepal.Width = 3.0) & (Species = "setosa")), # nolint + regex = "Filtering did not work" + ) + # styler: on +}) From 6e3182c7eb9602badffb5db94cf3cea8149c193d Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 10 Aug 2023 18:46:34 +0200 Subject: [PATCH 17/20] Implement `rowmean_n()` (#445) * draft mean_n() * desc, news * pkgdown * more performant than apply * apply more performant * finalize, add tests * no rounding by default * mean_n -> rowmean_n * address comments * fix test * fix * use rowMeans() * use .coerce_to_dataframe() --------- Co-authored-by: Etienne Bacher <52219252+etiennebacher@users.noreply.github.com> --- DESCRIPTION | 2 +- NAMESPACE | 1 + NEWS.md | 5 ++ R/rowmean_n.R | 101 ++++++++++++++++++++++++++++++++ _pkgdown.yaml | 1 + man/describe_distribution.Rd | 13 +++- man/rowmean_n.Rd | 72 +++++++++++++++++++++++ tests/testthat/test-rowmean_n.R | 26 ++++++++ 8 files changed, 217 insertions(+), 4 deletions(-) create mode 100644 R/rowmean_n.R create mode 100644 man/rowmean_n.Rd create mode 100644 tests/testthat/test-rowmean_n.R diff --git a/DESCRIPTION b/DESCRIPTION index 6a0264ee0..3c71a6343 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: datawizard Title: Easy Data Wrangling and Statistical Transformations -Version: 0.8.0.4 +Version: 0.8.0.5 Authors@R: c( person("Indrajeet", "Patil", , "patilindrajeet.science@gmail.com", role = "aut", comment = c(ORCID = "0000-0003-1995-6531", Twitter = "@patilindrajeets")), diff --git a/NAMESPACE b/NAMESPACE index 08ec43927..bb2d43766 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -272,6 +272,7 @@ export(reverse) export(reverse_scale) export(row_to_colnames) export(rowid_as_column) +export(rowmean_n) export(rownames_as_column) export(skewness) export(slide) diff --git a/NEWS.md b/NEWS.md index c3aee3b24..529ec1398 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,10 @@ # datawizard (devel) +NEW FUNCTIONS + +* `rowmean_n()`, to compute row means if row contains at least `n` non-missing + values. + CHANGES * `recode_into()` gains an `overwrite` argument to skip overwriting already diff --git a/R/rowmean_n.R b/R/rowmean_n.R new file mode 100644 index 000000000..ab47cf511 --- /dev/null +++ b/R/rowmean_n.R @@ -0,0 +1,101 @@ +#' @title Row means with minimum amount of valid values +#' @name rowmean_n +#' @description This function is similar to the SPSS `MEAN.n` function and computes +#' row means from a data frame or matrix if at least `n` values of a row are +#' valid (and not `NA`). +#' +#' @param data A data frame with at least two columns, where row means are applied. +#' @param n A numeric value of length 1. May either be +#' - a numeric value that indicates the amount of valid values per row to +#' calculate the row mean; +#' - or a value between 0 and 1, indicating a proportion of valid values per +#' row to calculate the row mean (see 'Details'). +#' +#' If a row's sum of valid values is less than `n`, `NA` will be returned. +#' @param digits Numeric value indicating the number of decimal places to be +#' used for rounding mean values. Negative values are allowed (see 'Details'). +#' By default, `digits = NULL` and no rounding is used. +#' @param verbose Toggle warnings. +#' +#' @return A vector with row means for those rows with at least `n` valid values. +#' +#' @details Rounding to a negative number of `digits` means rounding to a power of +#' ten, for example `rowmean_n(df, 3, digits = -2)` rounds to the nearest hundred. +#' For `n`, must be a numeric value from `0` to `ncol(data)`. If a row in the +#' data frame has at least `n` non-missing values, the row mean is returned. If +#' `n` is a non-integer value from 0 to 1, `n` is considered to indicate the +#' proportion of required non-missing values per row. E.g., if `n = 0.75`, a +#' row must have at least `ncol(data) * n` non-missing values for the row mean +#' to be calculated. See 'Examples'. +#' +#' @examples +#' dat <- data.frame( +#' c1 = c(1, 2, NA, 4), +#' c2 = c(NA, 2, NA, 5), +#' c3 = c(NA, 4, NA, NA), +#' c4 = c(2, 3, 7, 8) +#' ) +#' +#' # needs at least 4 non-missing values per row +#' rowmean_n(dat, 4) # 1 valid return value +#' +#' # needs at least 3 non-missing values per row +#' rowmean_n(dat, 3) # 2 valid return values +#' +#' # needs at least 2 non-missing values per row +#' rowmean_n(dat, 2) +#' +#' # needs at least 1 non-missing value per row +#' rowmean_n(dat, 1) # all means are shown +#' +#' # needs at least 50% of non-missing values per row +#' rowmean_n(dat, 0.5) # 3 valid return values +#' +#' # needs at least 75% of non-missing values per row +#' rowmean_n(dat, 0.75) # 2 valid return values +#' +#' @export +rowmean_n <- function(data, n, digits = NULL, verbose = TRUE) { + data <- .coerce_to_dataframe(data) + + # n must be a numeric, non-missing value + if (is.null(n) || all(is.na(n)) || !is.numeric(n) || length(n) > 1) { + insight::format_error("`n` must be a numeric value of length 1.") + } + + # make sure we only have numeric values + numeric_columns <- vapply(data, is.numeric, TRUE) + if (!all(numeric_columns)) { + if (verbose) { + insight::format_alert("Only numeric columns are considered for calculation.") + } + data <- data[numeric_columns] + } + + # check if we have a data framme with at least two columns + if (ncol(data) < 2) { + insight::format_error("`data` must be a data frame with at least two numeric columns.") + } + + # is 'n' indicating a proportion? + decimals <- n %% 1 + if (decimals != 0) { + n <- round(ncol(data) * decimals) + } + + # n may not be larger as df's amount of columns + if (ncol(data) < n) { + insight::format_error("`n` must be smaller or equal to number of columns in data frame.") + } + + # row means + to_na <- rowSums(is.na(data)) > ncol(data) - n + out <- rowMeans(data, na.rm = TRUE) + out[to_na] <- NA + + # round, if requested + if (!is.null(digits) && !all(is.na(digits))) { + out <- round(out, digits = digits) + } + out +} diff --git a/_pkgdown.yaml b/_pkgdown.yaml index 038a405a3..9d321aa78 100644 --- a/_pkgdown.yaml +++ b/_pkgdown.yaml @@ -64,6 +64,7 @@ reference: - smoothness - skewness - weighted_mean + - rowmean_n - mean_sd - title: Convert and Replace Data diff --git a/man/describe_distribution.Rd b/man/describe_distribution.Rd index a23069eea..fd229567d 100644 --- a/man/describe_distribution.Rd +++ b/man/describe_distribution.Rd @@ -50,9 +50,14 @@ describe_distribution(x, ...) \item{...}{Additional arguments to be passed to or from methods.} -\item{centrality}{The point-estimates (centrality indices) to compute. Character (vector) or list with one or more of these options: \code{"median"}, \code{"mean"}, \code{"MAP"} or \code{"all"}.} +\item{centrality}{The point-estimates (centrality indices) to compute. Character +(vector) or list with one or more of these options: \code{"median"}, \code{"mean"}, \code{"MAP"} +(see \code{\link[bayestestR:map_estimate]{map_estimate()}}), \code{"trimmed"} (which is just \code{mean(x, trim = threshold)}), +\code{"mode"} or \code{"all"}.} -\item{dispersion}{Logical, if \code{TRUE}, computes indices of dispersion related to the estimate(s) (\code{SD} and \code{MAD} for \code{mean} and \code{median}, respectively).} +\item{dispersion}{Logical, if \code{TRUE}, computes indices of dispersion related +to the estimate(s) (\code{SD} and \code{MAD} for \code{mean} and \code{median}, respectively). +Dispersion is not available for \code{"MAP"} or \code{"mode"} centrality indices.} \item{iqr}{Logical, if \code{TRUE}, the interquartile range is calculated (based on \code{\link[stats:IQR]{stats::IQR()}}, using \code{type = 6}).} @@ -71,7 +76,9 @@ the first centrality index (which is typically the median).} \item{iterations}{The number of bootstrap replicates for computing confidence intervals. Only applies when \code{ci} is not \code{NULL}.} -\item{threshold}{For \code{centrality = "trimmed"} (i.e. trimmed mean), indicates the fraction (0 to 0.5) of observations to be trimmed from each end of the vector before the mean is computed.} +\item{threshold}{For \code{centrality = "trimmed"} (i.e. trimmed mean), indicates +the fraction (0 to 0.5) of observations to be trimmed from each end of the +vector before the mean is computed.} \item{verbose}{Toggle warnings and messages.} diff --git a/man/rowmean_n.Rd b/man/rowmean_n.Rd new file mode 100644 index 000000000..df340eed3 --- /dev/null +++ b/man/rowmean_n.Rd @@ -0,0 +1,72 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/rowmean_n.R +\name{rowmean_n} +\alias{rowmean_n} +\title{Row means with minimum amount of valid values} +\usage{ +rowmean_n(data, n, digits = NULL, verbose = TRUE) +} +\arguments{ +\item{data}{A data frame with at least two columns, where row means are applied.} + +\item{n}{A numeric value of length 1. May either be +\itemize{ +\item a numeric value that indicates the amount of valid values per row to +calculate the row mean; +\item or a value between 0 and 1, indicating a proportion of valid values per +row to calculate the row mean (see 'Details'). +} + +If a row's sum of valid values is less than \code{n}, \code{NA} will be returned.} + +\item{digits}{Numeric value indicating the number of decimal places to be +used for rounding mean values. Negative values are allowed (see 'Details'). +By default, \code{digits = NULL} and no rounding is used.} + +\item{verbose}{Toggle warnings.} +} +\value{ +A vector with row means for those rows with at least \code{n} valid values. +} +\description{ +This function is similar to the SPSS \code{MEAN.n} function and computes +row means from a data frame or matrix if at least \code{n} values of a row are +valid (and not \code{NA}). +} +\details{ +Rounding to a negative number of \code{digits} means rounding to a power of +ten, for example \code{rowmean_n(df, 3, digits = -2)} rounds to the nearest hundred. +For \code{n}, must be a numeric value from \code{0} to \code{ncol(data)}. If a row in the +data frame has at least \code{n} non-missing values, the row mean is returned. If +\code{n} is a non-integer value from 0 to 1, \code{n} is considered to indicate the +proportion of required non-missing values per row. E.g., if \code{n = 0.75}, a +row must have at least \code{ncol(data) * n} non-missing values for the row mean +to be calculated. See 'Examples'. +} +\examples{ +dat <- data.frame( + c1 = c(1, 2, NA, 4), + c2 = c(NA, 2, NA, 5), + c3 = c(NA, 4, NA, NA), + c4 = c(2, 3, 7, 8) +) + +# needs at least 4 non-missing values per row +rowmean_n(dat, 4) # 1 valid return value + +# needs at least 3 non-missing values per row +rowmean_n(dat, 3) # 2 valid return values + +# needs at least 2 non-missing values per row +rowmean_n(dat, 2) + +# needs at least 1 non-missing value per row +rowmean_n(dat, 1) # all means are shown + +# needs at least 50\% of non-missing values per row +rowmean_n(dat, 0.5) # 3 valid return values + +# needs at least 75\% of non-missing values per row +rowmean_n(dat, 0.75) # 2 valid return values + +} diff --git a/tests/testthat/test-rowmean_n.R b/tests/testthat/test-rowmean_n.R new file mode 100644 index 000000000..a17996ff6 --- /dev/null +++ b/tests/testthat/test-rowmean_n.R @@ -0,0 +1,26 @@ +test_that("rowmean_n", { + d_mn <- data.frame( + c1 = c(1, 2, NA, 4), + c2 = c(NA, 2, NA, 5), + c3 = c(NA, 4, NA, NA), + c4 = c(2, 3, 7, 8) + ) + expect_equal(rowmean_n(d_mn, 4), c(NA, 2.75, NA, NA), tolerance = 1e-3) + expect_equal(rowmean_n(d_mn, 3), c(NA, 2.75, NA, 5.66667), tolerance = 1e-3) + expect_equal(rowmean_n(d_mn, 2), c(1.5, 2.75, NA, 5.66667), tolerance = 1e-3) + expect_equal(rowmean_n(d_mn, 1), c(1.5, 2.75, 7, 5.66667), tolerance = 1e-3) + expect_equal(rowmean_n(d_mn, 0.5), c(1.5, 2.75, NA, 5.66667), tolerance = 1e-3) + expect_equal(rowmean_n(d_mn, 0.75), c(NA, 2.75, NA, 5.66667), tolerance = 1e-3) + expect_equal(rowmean_n(d_mn, 2, digits = 1), c(1.5, 2.8, NA, 5.7), tolerance = 1e-1) +}) + +test_that("rowmean_n, errors or messages", { + data(iris) + expect_error(rowmean_n(5, n = 1), regex = "`data` must be") + expect_error(rowmean_n(iris[1], n = 1), regex = "two numeric") + expect_error(rowmean_n(iris, n = NULL), regex = "numeric value") + expect_error(rowmean_n(iris, n = 1:4), regex = "numeric value") + expect_error(rowmean_n(iris, n = "a"), regex = "numeric value") + expect_message(rowmean_n(iris[1:3, ], n = 3), regex = "Only numeric") + expect_silent(rowmean_n(iris[1:3, ], n = 3, verbose = FALSE)) +}) From 57b193d56d46ae98de2d21007a1c5b1cfd457adc Mon Sep 17 00:00:00 2001 From: Daniel Date: Sat, 12 Aug 2023 15:46:07 +0200 Subject: [PATCH 18/20] Implement `means_by_group()` (#446) * draft grouped mean * add methods * fix * fix * docs * fix printing issues * fix check issues * fix * news, desc * fix issues * add p-value * emmeans to suggests * add ci * fix, tests * snapshots * docs * add tests * docs * round weighted N * fix for weights * skip test if emmeans not installed * typo in news * fix typos in docs --------- Co-authored-by: Etienne Bacher <52219252+etiennebacher@users.noreply.github.com> --- DESCRIPTION | 3 +- NAMESPACE | 7 + NEWS.md | 3 + R/means_by_group.R | 294 ++++++++++++++++++ _pkgdown.yaml | 1 + man/means_by_group.Rd | 125 ++++++++ tests/testthat/_snaps/means_by_group.md | 172 ++++++++++ .../testthat/_snaps/windows/means_by_group.md | 34 ++ tests/testthat/test-labelled_data.R | 36 ++- tests/testthat/test-means_by_group.R | 21 ++ 10 files changed, 678 insertions(+), 18 deletions(-) create mode 100644 R/means_by_group.R create mode 100644 man/means_by_group.Rd create mode 100644 tests/testthat/_snaps/means_by_group.md create mode 100644 tests/testthat/_snaps/windows/means_by_group.md create mode 100644 tests/testthat/test-means_by_group.R diff --git a/DESCRIPTION b/DESCRIPTION index 3c71a6343..d86f68812 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: datawizard Title: Easy Data Wrangling and Statistical Transformations -Version: 0.8.0.5 +Version: 0.8.0.6 Authors@R: c( person("Indrajeet", "Patil", , "patilindrajeet.science@gmail.com", role = "aut", comment = c(ORCID = "0000-0003-1995-6531", Twitter = "@patilindrajeets")), @@ -43,6 +43,7 @@ Suggests: data.table, dplyr (>= 1.0), effectsize, + emmeans, gamm4, ggplot2, gt, diff --git a/NAMESPACE b/NAMESPACE index bb2d43766..fac5fbaf6 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -67,6 +67,7 @@ S3method(describe_distribution,numeric) S3method(format,data_codebook) S3method(format,dw_data_peek) S3method(format,dw_data_tabulate) +S3method(format,dw_groupmeans) S3method(format,parameters_distribution) S3method(kurtosis,data.frame) S3method(kurtosis,default) @@ -76,6 +77,9 @@ S3method(labels_to_levels,data.frame) S3method(labels_to_levels,default) S3method(labels_to_levels,factor) S3method(makepredictcall,dw_transformer) +S3method(means_by_group,data.frame) +S3method(means_by_group,default) +S3method(means_by_group,numeric) S3method(normalize,data.frame) S3method(normalize,factor) S3method(normalize,grouped_df) @@ -86,6 +90,8 @@ S3method(print,data_codebook) S3method(print,dw_data_peek) S3method(print,dw_data_tabulate) S3method(print,dw_data_tabulates) +S3method(print,dw_groupmeans) +S3method(print,dw_groupmeans_list) S3method(print,dw_transformer) S3method(print,parameters_distribution) S3method(print,parameters_kurtosis) @@ -252,6 +258,7 @@ export(get_columns) export(kurtosis) export(labels_to_levels) export(mean_sd) +export(means_by_group) export(median_mad) export(normalize) export(print_html) diff --git a/NEWS.md b/NEWS.md index 529ec1398..858b95f93 100644 --- a/NEWS.md +++ b/NEWS.md @@ -5,6 +5,9 @@ NEW FUNCTIONS * `rowmean_n()`, to compute row means if row contains at least `n` non-missing values. +* `means_by_group()`, to compute mean values of variables, grouped by levels + of specified factors. + CHANGES * `recode_into()` gains an `overwrite` argument to skip overwriting already diff --git a/R/means_by_group.R b/R/means_by_group.R new file mode 100644 index 000000000..1d3f6fd52 --- /dev/null +++ b/R/means_by_group.R @@ -0,0 +1,294 @@ +#' @title Summary of mean values by group +#' @name means_by_group +#' +#' @description Computes summary table of means by groups. +#' +#' @param x A vector or a data frame. +#' @param group If `x` is a numeric vector, `group` should be a factor that +#' indicates the group-classifying categories. If `x` is a data frame, `group` +#' should be a character string, naming the variable in `x` that is used for +#' grouping. Numeric vectors are coerced to factors. Not that `group` should +#' only refer to a single variable. +#' @param ci Level of confidence interval for mean estimates. Default is `0.95`. +#' Use `ci = NA` to suppress confidence intervals. +#' @param weights If `x` is a numeric vector, `weights` should be a vector of +#' weights that will be applied to weight all observations. If `x` is a data +#' frame, `weights` can also be a character string indicating the name of the +#' variable in `x` that should be used for weighting. Default is `NULL`, so no +#' weights are used. +#' @param digits Optional scalar, indicating the amount of digits after decimal +#' point when rounding estimates and values. +#' @param ... Currently not used +#' @inheritParams find_columns +#' +#' @return A data frame with information on mean and further summary statistics +#' for each sub-group. +#' +#' @details This function is comparable to `aggregate(x, group, mean)`, but provides +#' some further information, including summary statistics from a One-Way-ANOVA +#' using `x` as dependent and `group` as independent variable. [`emmeans::contrast()`] +#' is used to get p-values for each sub-group. P-values indicate whether each +#' group-mean is significantly different from the total mean. +#' +#' @examples +#' data(efc) +#' means_by_group(efc, "c12hour", "e42dep") +#' +#' data(iris) +#' means_by_group(iris, "Sepal.Width", "Species") +#' +#' # weighting +#' efc$weight <- abs(rnorm(n = nrow(efc), mean = 1, sd = .5)) +#' means_by_group(efc, "c12hour", "e42dep", weights = "weight") +#' @export +means_by_group <- function(x, ...) { + UseMethod("means_by_group") +} + + +#' @export +means_by_group.default <- function(x, ...) { + insight::format_error("`means_by_group()` does not work for objects of class `", class(x)[1], "`.") +} + + +#' @rdname means_by_group +#' @export +means_by_group.numeric <- function(x, + group = NULL, + ci = 0.95, + weights = NULL, + digits = NULL, + ...) { + # sanity check for arguments + + # "group" must be provided + if (is.null(group)) { + insight::format_error("Argument `group` is missing.") + } + + # group must be of same length as x + if (length(group) != length(x)) { + insight::format_error("Argument `group` must be of same length as `x`.") + } + + # if weights are provided, must be of same length as x + if (!is.null(weights) && length(weights) != length(x)) { + insight::format_error("Argument `weights` must be of same length as `x`.") + } + + # if weights are NULL, set weights to 1 + if (is.null(weights)) weights <- rep(1, length(x)) + + # retrieve labels + var_mean_label <- attr(x, "label", exact = TRUE) + var_grp_label <- attr(group, "label", exact = TRUE) + + # if no labels present, use variable names directly + if (is.null(var_mean_label)) { + var_mean_label <- deparse(substitute(x)) + } + if (is.null(var_grp_label)) { + var_grp_label <- deparse(substitute(group)) + } + + # coerce group to factor if numeric, or convert labels to levels, if factor + if (is.factor(group)) { + group <- tryCatch(labels_to_levels(group, verbose = FALSE), error = function(e) group) + } else { + group <- to_factor(group) + } + + data <- stats::na.omit(data.frame( + x = x, + group = group, + weights = weights, + stringsAsFactors = FALSE + )) + + # get grouped means table + out <- .means_by_group(data, ci = ci) + + # attributes + attr(out, "var_mean_label") <- var_mean_label + attr(out, "var_grp_label") <- var_grp_label + attr(out, "digits") <- digits + + class(out) <- c("dw_groupmeans", "data.frame") + out +} + + +#' @rdname means_by_group +#' @export +means_by_group.data.frame <- function(x, + select = NULL, + group = NULL, + ci = 0.95, + weights = NULL, + digits = NULL, + exclude = NULL, + ignore_case = FALSE, + regex = FALSE, + verbose = TRUE, + ...) { + # evaluate select/exclude, may be select-helpers + select <- .select_nse(select, + x, + exclude, + ignore_case, + regex = regex, + verbose = verbose + ) + + if (is.null(weights)) { + w <- NULL + } else if (is.character(weights)) { + w <- x[[weights]] + } else { + w <- weights + } + + out <- lapply(select, function(i) { + # if no labels present, use variable names directy + if (is.null(attr(x[[i]], "label", exact = TRUE))) { + attr(x[[i]], "label") <- i + } + if (is.null(attr(x[[group]], "label", exact = TRUE))) { + attr(x[[group]], "label") <- group + } + # compute means table + means_by_group(x[[i]], group = x[[group]], ci = ci, weights = w, digits = digits, ...) + }) + + class(out) <- c("dw_groupmeans_list", "list") + out +} + + +#' @keywords internal +.means_by_group <- function(data, ci = 0.95) { + # compute anova statistics for mean table + if (is.null(data$weights) || all(data$weights == 1)) { + fit <- stats::lm(x ~ group, data = data) + } else { + fit <- stats::lm(x ~ group, weights = data$weights, data = data) + } + + # summary table data + groups <- split(data$x, data$group) + group_weights <- split(data$weights, data$group) + out <- do.call(rbind, Map(function(x, w) { + data.frame( + Mean = weighted_mean(x, weights = w), + SD = weighted_sd(x, weights = w), + N = round(sum(w)), + stringsAsFactors = FALSE + ) + }, groups, group_weights)) + + # add group names + out$Category <- levels(data$group) + out$p <- out$CI_high <- out$CI_low <- NA + + # p-values of contrast-means + if (insight::check_if_installed("emmeans", quietly = TRUE)) { + # create summary table of contrasts, for p-values and confidence intervals + predicted <- emmeans::emmeans(fit, specs = "group", level = ci) + contrasts <- emmeans::contrast(predicted, method = "eff") + # add p-values and confidence intervals to "out" + if (!is.null(ci) && !is.na(ci)) { + summary_table <- as.data.frame(predicted) + out$CI_low <- summary_table$lower.CL + out$CI_high <- summary_table$upper.CL + } + summary_table <- as.data.frame(contrasts) + out$p <- summary_table$p.value + } + + # reorder columns + out <- out[c("Category", "Mean", "N", "SD", "CI_low", "CI_high", "p")] + + # finally, add total-row + out <- rbind( + out, + data.frame( + Category = "Total", + Mean = weighted_mean(data$x, weights = data$weights), + N = nrow(data), + SD = weighted_sd(data$x, weights = data$weights), + CI_low = NA, + CI_high = NA, + p = NA, + stringsAsFactors = FALSE + ) + ) + + # get anova statistics for mean table + sum.fit <- summary(fit) + + # r-squared values + r2 <- sum.fit$r.squared + r2.adj <- sum.fit$adj.r.squared + + # F-statistics + fstat <- sum.fit$fstatistic + pval <- stats::pf(fstat[1], fstat[2], fstat[3], lower.tail = FALSE) + + # copy as attributes + attr(out, "r2") <- r2 + attr(out, "ci") <- ci + attr(out, "adj.r2") <- r2.adj + attr(out, "fstat") <- fstat[1] + attr(out, "p.value") <- pval + + out +} + + +# methods ----------------- + +#' @export +format.dw_groupmeans <- function(x, digits = NULL, ...) { + if (is.null(digits)) { + digits <- attr(x, "digits", exact = TRUE) + } + if (is.null(digits)) { + digits <- 2 + } + x$N <- insight::format_value(x$N, digits = 0) + insight::format_table(remove_empty_columns(x), digits = digits, ...) +} + +#' @export +print.dw_groupmeans <- function(x, digits = NULL, ...) { + out <- format(x, digits = digits, ...) + + # caption + l1 <- attributes(x)$var_mean_label + l2 <- attributes(x)$var_grp_label + if (!is.null(l1) && !is.null(l2)) { + caption <- c(paste0("# Mean of ", l1, " by ", l2), "blue") + } else { + caption <- NULL + } + + # footer + footer <- paste0( + "\nAnova: R2=", insight::format_value(attributes(x)$r2, digits = 3), + "; adj.R2=", insight::format_value(attributes(x)$adj.r2, digits = 3), + "; F=", insight::format_value(attributes(x)$fstat, digits = 3), + "; ", insight::format_p(attributes(x)$p.value, whitespace = FALSE), + "\n" + ) + + cat(insight::export_table(out, caption = caption, footer = footer, ...)) +} + +#' @export +print.dw_groupmeans_list <- function(x, digits = NULL, ...) { + for (i in seq_along(x)) { + if (i > 1) cat("\n") + print(x[[i]], digits = digits, ...) + } +} diff --git a/_pkgdown.yaml b/_pkgdown.yaml index 9d321aa78..1da6b0661 100644 --- a/_pkgdown.yaml +++ b/_pkgdown.yaml @@ -59,6 +59,7 @@ reference: - data_codebook - data_tabulate - data_peek + - means_by_group - contains("distribution") - kurtosis - smoothness diff --git a/man/means_by_group.Rd b/man/means_by_group.Rd new file mode 100644 index 000000000..9434452ad --- /dev/null +++ b/man/means_by_group.Rd @@ -0,0 +1,125 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/means_by_group.R +\name{means_by_group} +\alias{means_by_group} +\alias{means_by_group.numeric} +\alias{means_by_group.data.frame} +\title{Summary of mean values by group} +\usage{ +means_by_group(x, ...) + +\method{means_by_group}{numeric}(x, group = NULL, ci = 0.95, weights = NULL, digits = NULL, ...) + +\method{means_by_group}{data.frame}( + x, + select = NULL, + group = NULL, + ci = 0.95, + weights = NULL, + digits = NULL, + exclude = NULL, + ignore_case = FALSE, + regex = FALSE, + verbose = TRUE, + ... +) +} +\arguments{ +\item{x}{A vector or a data frame.} + +\item{...}{Currently not used} + +\item{group}{If \code{x} is a numeric vector, \code{group} should be a factor that +indicates the group-classifying categories. If \code{x} is a data frame, \code{group} +should be a character string, naming the variable in \code{x} that is used for +grouping. Numeric vectors are coerced to factors. Not that \code{group} should +only refer to a single variable.} + +\item{ci}{Level of confidence interval for mean estimates. Default is \code{0.95}. +Use \code{ci = NA} to suppress confidence intervals.} + +\item{weights}{If \code{x} is a numeric vector, \code{weights} should be a vector of +weights that will be applied to weight all observations. If \code{x} is a data +frame, \code{weights} can also be a character string indicating the name of the +variable in \code{x} that should be used for weighting. Default is \code{NULL}, so no +weights are used.} + +\item{digits}{Optional scalar, indicating the amount of digits after decimal +point when rounding estimates and values.} + +\item{select}{Variables that will be included when performing the required +tasks. Can be either +\itemize{ +\item a variable specified as a literal variable name (e.g., \code{column_name}), +\item a string with the variable name (e.g., \code{"column_name"}), or a character +vector of variable names (e.g., \code{c("col1", "col2", "col3")}), +\item a formula with variable names (e.g., \code{~column_1 + column_2}), +\item a vector of positive integers, giving the positions counting from the left +(e.g. \code{1} or \code{c(1, 3, 5)}), +\item a vector of negative integers, giving the positions counting from the +right (e.g., \code{-1} or \code{-1:-3}), +\item one of the following select-helpers: \code{starts_with()}, \code{ends_with()}, +\code{contains()}, a range using \code{:} or \code{regex("")}. \code{starts_with()}, +\code{ends_with()}, and \code{contains()} accept several patterns, e.g +\code{starts_with("Sep", "Petal")}. +\item or a function testing for logical conditions, e.g. \code{is.numeric()} (or +\code{is.numeric}), or any user-defined function that selects the variables +for which the function returns \code{TRUE} (like: \code{foo <- function(x) mean(x) > 3}), +\item ranges specified via literal variable names, select-helpers (except +\code{regex()}) and (user-defined) functions can be negated, i.e. return +non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with("")}, +\code{-is.numeric} or \code{-(Sepal.Width:Petal.Length)}. \strong{Note:} Negation means +that matches are \emph{excluded}, and thus, the \code{exclude} argument can be +used alternatively. For instance, \code{select=-ends_with("Length")} (with +\code{-}) is equivalent to \code{exclude=ends_with("Length")} (no \code{-}). In case +negation should not work as expected, use the \code{exclude} argument instead. +} + +If \code{NULL}, selects all columns. Patterns that found no matches are silently +ignored, e.g. \code{find_columns(iris, select = c("Species", "Test"))} will just +return \code{"Species"}.} + +\item{exclude}{See \code{select}, however, column names matched by the pattern +from \code{exclude} will be excluded instead of selected. If \code{NULL} (the default), +excludes no columns.} + +\item{ignore_case}{Logical, if \code{TRUE} and when one of the select-helpers or +a regular expression is used in \code{select}, ignores lower/upper case in the +search pattern when matching against variable names.} + +\item{regex}{Logical, if \code{TRUE}, the search pattern from \code{select} will be +treated as regular expression. When \code{regex = TRUE}, select \emph{must} be a +character string (or a variable containing a character string) and is not +allowed to be one of the supported select-helpers or a character vector +of length > 1. \code{regex = TRUE} is comparable to using one of the two +select-helpers, \code{select = contains("")} or \code{select = regex("")}, however, +since the select-helpers may not work when called from inside other +functions (see 'Details'), this argument may be used as workaround.} + +\item{verbose}{Toggle warnings.} +} +\value{ +A data frame with information on mean and further summary statistics +for each sub-group. +} +\description{ +Computes summary table of means by groups. +} +\details{ +This function is comparable to \code{aggregate(x, group, mean)}, but provides +some further information, including summary statistics from a One-Way-ANOVA +using \code{x} as dependent and \code{group} as independent variable. \code{\link[emmeans:contrast]{emmeans::contrast()}} +is used to get p-values for each sub-group. P-values indicate whether each +group-mean is significantly different from the total mean. +} +\examples{ +data(efc) +means_by_group(efc, "c12hour", "e42dep") + +data(iris) +means_by_group(iris, "Sepal.Width", "Species") + +# weighting +efc$weight <- abs(rnorm(n = nrow(efc), mean = 1, sd = .5)) +means_by_group(efc, "c12hour", "e42dep", weights = "weight") +} diff --git a/tests/testthat/_snaps/means_by_group.md b/tests/testthat/_snaps/means_by_group.md new file mode 100644 index 000000000..78a43d8b4 --- /dev/null +++ b/tests/testthat/_snaps/means_by_group.md @@ -0,0 +1,172 @@ +# meany_by_group + + Code + means_by_group(efc, "c12hour", "e42dep") + Output + # Mean of average number of hours of care per week by elder's dependency + + Category | Mean | N | SD | 95% CI | p + ---------------------------------------------------------------------- + independent | 17.00 | 2 | 11.31 | [-68.46, 102.46] | 0.573 + slightly dependent | 34.25 | 4 | 29.97 | [-26.18, 94.68] | 0.626 + moderately dependent | 52.75 | 28 | 51.83 | [ 29.91, 75.59] | > .999 + severely dependent | 106.97 | 63 | 65.88 | [ 91.74, 122.19] | 0.001 + Total | 86.46 | 97 | 66.40 | | + + Anova: R2=0.186; adj.R2=0.160; F=7.098; p<.001 + +--- + + Code + means_by_group(efc, "c12hour", "e42dep", ci = 0.99) + Output + # Mean of average number of hours of care per week by elder's dependency + + Category | Mean | N | SD | 99% CI | p + ---------------------------------------------------------------------- + independent | 17.00 | 2 | 11.31 | [-96.17, 130.17] | 0.573 + slightly dependent | 34.25 | 4 | 29.97 | [-45.77, 114.27] | 0.626 + moderately dependent | 52.75 | 28 | 51.83 | [ 22.50, 83.00] | > .999 + severely dependent | 106.97 | 63 | 65.88 | [ 86.80, 127.13] | 0.001 + Total | 86.46 | 97 | 66.40 | | + + Anova: R2=0.186; adj.R2=0.160; F=7.098; p<.001 + +--- + + Code + means_by_group(efc, "c12hour", "e42dep", ci = NA) + Output + # Mean of average number of hours of care per week by elder's dependency + + Category | Mean | N | SD | p + --------------------------------------------------- + independent | 17.00 | 2 | 11.31 | 0.573 + slightly dependent | 34.25 | 4 | 29.97 | 0.626 + moderately dependent | 52.75 | 28 | 51.83 | > .999 + severely dependent | 106.97 | 63 | 65.88 | 0.001 + Total | 86.46 | 97 | 66.40 | + + Anova: R2=0.186; adj.R2=0.160; F=7.098; p<.001 + +--- + + Code + means_by_group(efc, c("neg_c_7", "c12hour"), "e42dep") + Output + # Mean of Negative impact with 7 items by elder's dependency + + Category | Mean | N | SD | 95% CI | p + ----------------------------------------------------------------- + independent | 11.00 | 2 | 0.00 | [ 5.00, 17.00] | 0.567 + slightly dependent | 10.00 | 4 | 3.16 | [ 5.76, 14.24] | 0.296 + moderately dependent | 13.71 | 28 | 3.14 | [12.11, 15.32] | 0.296 + severely dependent | 14.67 | 60 | 4.78 | [13.57, 15.76] | 0.108 + Total | 14.11 | 94 | 4.34 | | + + Anova: R2=0.063; adj.R2=0.032; F=2.009; p=0.118 + + # Mean of average number of hours of care per week by elder's dependency + + Category | Mean | N | SD | 95% CI | p + ---------------------------------------------------------------------- + independent | 17.00 | 2 | 11.31 | [-68.46, 102.46] | 0.573 + slightly dependent | 34.25 | 4 | 29.97 | [-26.18, 94.68] | 0.626 + moderately dependent | 52.75 | 28 | 51.83 | [ 29.91, 75.59] | > .999 + severely dependent | 106.97 | 63 | 65.88 | [ 91.74, 122.19] | 0.001 + Total | 86.46 | 97 | 66.40 | | + + Anova: R2=0.186; adj.R2=0.160; F=7.098; p<.001 + +--- + + Code + means_by_group(efc, c("neg_c_7", "c12hour"), "e42dep", ci = NA) + Output + # Mean of Negative impact with 7 items by elder's dependency + + Category | Mean | N | SD | p + ------------------------------------------------ + independent | 11.00 | 2 | 0.00 | 0.567 + slightly dependent | 10.00 | 4 | 3.16 | 0.296 + moderately dependent | 13.71 | 28 | 3.14 | 0.296 + severely dependent | 14.67 | 60 | 4.78 | 0.108 + Total | 14.11 | 94 | 4.34 | + + Anova: R2=0.063; adj.R2=0.032; F=2.009; p=0.118 + + # Mean of average number of hours of care per week by elder's dependency + + Category | Mean | N | SD | p + --------------------------------------------------- + independent | 17.00 | 2 | 11.31 | 0.573 + slightly dependent | 34.25 | 4 | 29.97 | 0.626 + moderately dependent | 52.75 | 28 | 51.83 | > .999 + severely dependent | 106.97 | 63 | 65.88 | 0.001 + Total | 86.46 | 97 | 66.40 | + + Anova: R2=0.186; adj.R2=0.160; F=7.098; p<.001 + +--- + + Code + means_by_group(efc, c("neg_c_7", "c12hour"), "e42dep", ci = 0.99) + Output + # Mean of Negative impact with 7 items by elder's dependency + + Category | Mean | N | SD | 99% CI | p + ----------------------------------------------------------------- + independent | 11.00 | 2 | 0.00 | [ 3.05, 18.95] | 0.567 + slightly dependent | 10.00 | 4 | 3.16 | [ 4.38, 15.62] | 0.296 + moderately dependent | 13.71 | 28 | 3.14 | [11.59, 15.84] | 0.296 + severely dependent | 14.67 | 60 | 4.78 | [13.22, 16.12] | 0.108 + Total | 14.11 | 94 | 4.34 | | + + Anova: R2=0.063; adj.R2=0.032; F=2.009; p=0.118 + + # Mean of average number of hours of care per week by elder's dependency + + Category | Mean | N | SD | 99% CI | p + ---------------------------------------------------------------------- + independent | 17.00 | 2 | 11.31 | [-96.17, 130.17] | 0.573 + slightly dependent | 34.25 | 4 | 29.97 | [-45.77, 114.27] | 0.626 + moderately dependent | 52.75 | 28 | 51.83 | [ 22.50, 83.00] | > .999 + severely dependent | 106.97 | 63 | 65.88 | [ 86.80, 127.13] | 0.001 + Total | 86.46 | 97 | 66.40 | | + + Anova: R2=0.186; adj.R2=0.160; F=7.098; p<.001 + +--- + + Code + means_by_group(efc$c12hour, efc$e42dep) + Output + # Mean of average number of hours of care per week by elder's dependency + + Category | Mean | N | SD | 95% CI | p + ---------------------------------------------------------------------- + independent | 17.00 | 2 | 11.31 | [-68.46, 102.46] | 0.573 + slightly dependent | 34.25 | 4 | 29.97 | [-26.18, 94.68] | 0.626 + moderately dependent | 52.75 | 28 | 51.83 | [ 29.91, 75.59] | > .999 + severely dependent | 106.97 | 63 | 65.88 | [ 91.74, 122.19] | 0.001 + Total | 86.46 | 97 | 66.40 | | + + Anova: R2=0.186; adj.R2=0.160; F=7.098; p<.001 + +--- + + Code + means_by_group(efc$c12hour, efc$e42dep, ci = NA) + Output + # Mean of average number of hours of care per week by elder's dependency + + Category | Mean | N | SD | p + --------------------------------------------------- + independent | 17.00 | 2 | 11.31 | 0.573 + slightly dependent | 34.25 | 4 | 29.97 | 0.626 + moderately dependent | 52.75 | 28 | 51.83 | > .999 + severely dependent | 106.97 | 63 | 65.88 | 0.001 + Total | 86.46 | 97 | 66.40 | + + Anova: R2=0.186; adj.R2=0.160; F=7.098; p<.001 + diff --git a/tests/testthat/_snaps/windows/means_by_group.md b/tests/testthat/_snaps/windows/means_by_group.md new file mode 100644 index 000000000..198069da9 --- /dev/null +++ b/tests/testthat/_snaps/windows/means_by_group.md @@ -0,0 +1,34 @@ +# meany_by_group, weighted + + Code + means_by_group(efc, "c12hour", "e42dep", weights = "weight") + Output + # Mean of average number of hours of care per week by elder's dependency + + Category | Mean | N | SD | 95% CI | p + ---------------------------------------------------------------------- + independent | 16.92 | 3 | 11.31 | [-60.82, 94.66] | 0.486 + slightly dependent | 33.56 | 4 | 29.75 | [-26.93, 94.05] | 0.593 + moderately dependent | 52.74 | 26 | 54.44 | [ 28.71, 76.76] | 0.996 + severely dependent | 108.08 | 67 | 65.40 | [ 93.01, 123.16] | < .001 + Total | 88.11 | 97 | 67.01 | | + + Anova: R2=0.191; adj.R2=0.165; F=7.329; p<.001 + +--- + + Code + means_by_group(efc, "c12hour", "e42dep", weights = "weight", ci = NA) + Output + # Mean of average number of hours of care per week by elder's dependency + + Category | Mean | N | SD | p + --------------------------------------------------- + independent | 16.92 | 3 | 11.31 | 0.486 + slightly dependent | 33.56 | 4 | 29.75 | 0.593 + moderately dependent | 52.74 | 26 | 54.44 | 0.996 + severely dependent | 108.08 | 67 | 65.40 | < .001 + Total | 88.11 | 97 | 67.01 | + + Anova: R2=0.191; adj.R2=0.165; F=7.329; p<.001 + diff --git a/tests/testthat/test-labelled_data.R b/tests/testthat/test-labelled_data.R index b0f92c730..0b7e37a4d 100644 --- a/tests/testthat/test-labelled_data.R +++ b/tests/testthat/test-labelled_data.R @@ -4,13 +4,13 @@ data(efc, package = "datawizard") test_that("reverse, labels preserved", { # factor, label - expect_equal( + expect_identical( attr(reverse(efc$e42dep), "label", exact = TRUE), "elder's dependency" ) # factor, labels - expect_equal( - names(attr(reverse(efc$e42dep), "labels", exact = TRUE)), + expect_named( + attr(reverse(efc$e42dep), "labels", exact = TRUE), names(attr(efc$e42dep, "labels", exact = TRUE)) ) expect_equal( @@ -19,13 +19,13 @@ test_that("reverse, labels preserved", { ignore_attr = TRUE ) # numeric - expect_equal( - names(attr(reverse(efc$c12hour), "labels", exact = TRUE)), + expect_named( + attr(reverse(efc$c12hour), "labels", exact = TRUE), names(attr(efc$c12hour, "labels", exact = TRUE)) ) # data frame - labels <- sapply(reverse(efc), function(i) attr(i, "label", exact = TRUE)) - expect_equal( + labels <- sapply(reverse(efc), attr, which = "label", exact = TRUE) + expect_identical( labels, c( c12hour = "average number of hours of care per week", @@ -42,8 +42,8 @@ test_that("reverse, labels preserved", { # data_merge ----------------------------------- test_that("data_merge, labels preserved", { - labels <- sapply(data_merge(efc[c(1:2)], efc[c(3:4)], verbose = FALSE), function(i) attr(i, "label", exact = TRUE)) - expect_equal( + labels <- sapply(data_merge(efc[1:2], efc[3:4], verbose = FALSE), attr, which = "label", exact = TRUE) + expect_identical( labels, c( c12hour = "average number of hours of care per week", @@ -72,8 +72,8 @@ test_that("data_extract, labels preserved", { ignore_attr = TRUE ) # data frame - labels <- sapply(data_extract(efc, select = c("e42dep", "c172code")), function(i) attr(i, "label", exact = TRUE)) - expect_equal( + labels <- sapply(data_extract(efc, select = c("e42dep", "c172code")), attr, which = "label", exact = TRUE) + expect_identical( labels, c(e42dep = "elder's dependency", c172code = "carer's level of education") ) @@ -142,8 +142,8 @@ test_that("data_rename, labels preserved", { ignore_attr = TRUE ) # data frame - labels <- sapply(data_remove(efc, starts_with("c1")), function(i) attr(i, "label", exact = TRUE)) - expect_equal( + labels <- sapply(data_remove(efc, starts_with("c1")), attr, which = "label", exact = TRUE) + expect_identical( labels, c(e16sex = "elder's gender", e42dep = "elder's dependency", neg_c_7 = "Negative impact with 7 items") ) @@ -255,12 +255,12 @@ test_that("data_match, labels preserved", { test_that("data_filter, labels preserved", { x <- data_filter(efc, c172code == 1 & c12hour > 40) # factor - expect_equal( + expect_identical( attr(x$e42dep, "label", exact = TRUE), attr(efc$e42dep, "label", exact = TRUE) ) # numeric - expect_equal( + expect_identical( attr(x$c12hour, "label", exact = TRUE), attr(efc$c12hour, "label", exact = TRUE) ) @@ -271,7 +271,9 @@ test_that("data_filter, labels preserved", { # convert_to_na ----------------------------------- test_that("convert_to_na, labels preserved", { - expect_message(x <- convert_to_na(efc, na = c(2, "2"), select = starts_with("e"))) + expect_message({ + x <- convert_to_na(efc, na = c(2, "2"), select = starts_with("e")) + }) # factor expect_equal( attr(x$e42dep, "label", exact = TRUE), @@ -301,7 +303,7 @@ test_that("convert_to_na, labels preserved", { ) # drop unused value labels x <- convert_to_na(efc$c172code, na = 2) - expect_equal( + expect_identical( attr(x, "labels", exact = TRUE), c(`low level of education` = 1, `high level of education` = 3) ) diff --git a/tests/testthat/test-means_by_group.R b/tests/testthat/test-means_by_group.R new file mode 100644 index 000000000..49226fa34 --- /dev/null +++ b/tests/testthat/test-means_by_group.R @@ -0,0 +1,21 @@ +test_that("mean_by_group", { + skip_if_not_installed("emmeans") + data(efc) + expect_snapshot(means_by_group(efc, "c12hour", "e42dep")) + expect_snapshot(means_by_group(efc, "c12hour", "e42dep", ci = 0.99)) + expect_snapshot(means_by_group(efc, "c12hour", "e42dep", ci = NA)) + expect_snapshot(means_by_group(efc, c("neg_c_7", "c12hour"), "e42dep")) + expect_snapshot(means_by_group(efc, c("neg_c_7", "c12hour"), "e42dep", ci = NA)) + expect_snapshot(means_by_group(efc, c("neg_c_7", "c12hour"), "e42dep", ci = 0.99)) + expect_snapshot(means_by_group(efc$c12hour, efc$e42dep)) + expect_snapshot(means_by_group(efc$c12hour, efc$e42dep, ci = NA)) +}) + +test_that("mean_by_group, weighted", { + skip_if_not_installed("emmeans") + data(efc) + set.seed(123) + efc$weight <- abs(rnorm(n = nrow(efc), mean = 1, sd = 0.5)) + expect_snapshot(means_by_group(efc, "c12hour", "e42dep", weights = "weight"), variant = "windows") + expect_snapshot(means_by_group(efc, "c12hour", "e42dep", weights = "weight", ci = NA), variant = "windows") +}) From c8f8a17fb4343bc6e3a9dedf9f6facb1e5f2f318 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Th=C3=A9riault?= <13123390+rempsyc@users.noreply.github.com> Date: Mon, 14 Aug 2023 04:47:14 -0400 Subject: [PATCH 19/20] Bump `insight` version for `.get_dep_version` (#449) bump version for `.get_dep_version` --- DESCRIPTION | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index d86f68812..6414d5fba 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -33,7 +33,7 @@ BugReports: https://github.com/easystats/datawizard/issues Depends: R (>= 3.6) Imports: - insight (>= 0.19.1), + insight (>= 0.19.3.2), stats, utils Suggests: @@ -78,3 +78,4 @@ Config/Needs/website: rstudio/bslib, r-lib/pkgdown, easystats/easystatstemplate +Remotes: easystats/insight From a1e06207a3e8f866af714154f08ad24780be2246 Mon Sep 17 00:00:00 2001 From: Etienne Bacher <52219252+etiennebacher@users.noreply.github.com> Date: Thu, 17 Aug 2023 08:27:26 +0200 Subject: [PATCH 20/20] Switch from GPL-3 to MIT license (#450) * switch from GPL-3 to MIT license * remove license from rbuildignore * bump NEWS and description [skip ci] --- .Rbuildignore | 1 - DESCRIPTION | 4 +- LICENSE | 676 +------------------------------------------------- LICENSE.md | 21 ++ NEWS.md | 2 + 5 files changed, 27 insertions(+), 677 deletions(-) create mode 100644 LICENSE.md diff --git a/.Rbuildignore b/.Rbuildignore index 74fc7c855..d245bf8c3 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -46,6 +46,5 @@ references.bib ^hextools/. ^WIP/. ^CRAN-SUBMISSION$ -^LICENSE$ docs ^.dev$ diff --git a/DESCRIPTION b/DESCRIPTION index 6414d5fba..1c68c50f1 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: datawizard Title: Easy Data Wrangling and Statistical Transformations -Version: 0.8.0.6 +Version: 0.8.0.7 Authors@R: c( person("Indrajeet", "Patil", , "patilindrajeet.science@gmail.com", role = "aut", comment = c(ORCID = "0000-0003-1995-6531", Twitter = "@patilindrajeets")), @@ -27,7 +27,7 @@ Description: A lightweight package to assist in key steps involved in any data (3) compute statistical summaries of data properties and distributions. It is also the data wrangling backend for packages in 'easystats' ecosystem. References: Patil et al. (2022) . -License: GPL (>= 3) +License: MIT + file LICENSE URL: https://easystats.github.io/datawizard/ BugReports: https://github.com/easystats/datawizard/issues Depends: diff --git a/LICENSE b/LICENSE index 61d18602b..847d54a99 100644 --- a/LICENSE +++ b/LICENSE @@ -1,674 +1,2 @@ -GNU GENERAL PUBLIC LICENSE - Version 3, 29 June 2007 - - Copyright (C) 2007 Free Software Foundation, Inc. - Everyone is permitted to copy and distribute verbatim copies - of this license document, but changing it is not allowed. - - Preamble - - The GNU General Public License is a free, copyleft license for -software and other kinds of works. - - The licenses for most software and other practical works are designed -to take away your freedom to share and change the works. By contrast, -the GNU General Public License is intended to guarantee your freedom to -share and change all versions of a program--to make sure it remains free -software for all its users. We, the Free Software Foundation, use the -GNU General Public License for most of our software; it applies also to -any other work released this way by its authors. You can apply it to -your programs, too. - - When we speak of free software, we are referring to freedom, not -price. Our General Public Licenses are designed to make sure that you -have the freedom to distribute copies of free software (and charge for -them if you wish), that you receive source code or can get it if you -want it, that you can change the software or use pieces of it in new -free programs, and that you know you can do these things. - - To protect your rights, we need to prevent others from denying you -these rights or asking you to surrender the rights. Therefore, you have -certain responsibilities if you distribute copies of the software, or if -you modify it: responsibilities to respect the freedom of others. - - For example, if you distribute copies of such a program, whether -gratis or for a fee, you must pass on to the recipients the same -freedoms that you received. You must make sure that they, too, receive -or can get the source code. And you must show them these terms so they -know their rights. - - Developers that use the GNU GPL protect your rights with two steps: -(1) assert copyright on the software, and (2) offer you this License -giving you legal permission to copy, distribute and/or modify it. - - For the developers' and authors' protection, the GPL clearly explains -that there is no warranty for this free software. For both users' and -authors' sake, the GPL requires that modified versions be marked as -changed, so that their problems will not be attributed erroneously to -authors of previous versions. - - Some devices are designed to deny users access to install or run -modified versions of the software inside them, although the manufacturer -can do so. This is fundamentally incompatible with the aim of -protecting users' freedom to change the software. The systematic -pattern of such abuse occurs in the area of products for individuals to -use, which is precisely where it is most unacceptable. Therefore, we -have designed this version of the GPL to prohibit the practice for those -products. If such problems arise substantially in other domains, we -stand ready to extend this provision to those domains in future versions -of the GPL, as needed to protect the freedom of users. - - Finally, every program is threatened constantly by software patents. -States should not allow patents to restrict development and use of -software on general-purpose computers, but in those that do, we wish to -avoid the special danger that patents applied to a free program could -make it effectively proprietary. To prevent this, the GPL assures that -patents cannot be used to render the program non-free. - - The precise terms and conditions for copying, distribution and -modification follow. - - TERMS AND CONDITIONS - - 0. Definitions. - - "This License" refers to version 3 of the GNU General Public License. - - "Copyright" also means copyright-like laws that apply to other kinds of -works, such as semiconductor masks. - - "The Program" refers to any copyrightable work licensed under this -License. Each licensee is addressed as "you". "Licensees" and -"recipients" may be individuals or organizations. - - To "modify" a work means to copy from or adapt all or part of the work -in a fashion requiring copyright permission, other than the making of an -exact copy. The resulting work is called a "modified version" of the -earlier work or a work "based on" the earlier work. - - A "covered work" means either the unmodified Program or a work based -on the Program. - - To "propagate" a work means to do anything with it that, without -permission, would make you directly or secondarily liable for -infringement under applicable copyright law, except executing it on a -computer or modifying a private copy. Propagation includes copying, -distribution (with or without modification), making available to the -public, and in some countries other activities as well. - - To "convey" a work means any kind of propagation that enables other -parties to make or receive copies. Mere interaction with a user through -a computer network, with no transfer of a copy, is not conveying. - - An interactive user interface displays "Appropriate Legal Notices" -to the extent that it includes a convenient and prominently visible -feature that (1) displays an appropriate copyright notice, and (2) -tells the user that there is no warranty for the work (except to the -extent that warranties are provided), that licensees may convey the -work under this License, and how to view a copy of this License. If -the interface presents a list of user commands or options, such as a -menu, a prominent item in the list meets this criterion. - - 1. Source Code. - - The "source code" for a work means the preferred form of the work -for making modifications to it. "Object code" means any non-source -form of a work. - - A "Standard Interface" means an interface that either is an official -standard defined by a recognized standards body, or, in the case of -interfaces specified for a particular programming language, one that -is widely used among developers working in that language. - - The "System Libraries" of an executable work include anything, other -than the work as a whole, that (a) is included in the normal form of -packaging a Major Component, but which is not part of that Major -Component, and (b) serves only to enable use of the work with that -Major Component, or to implement a Standard Interface for which an -implementation is available to the public in source code form. A -"Major Component", in this context, means a major essential component -(kernel, window system, and so on) of the specific operating system -(if any) on which the executable work runs, or a compiler used to -produce the work, or an object code interpreter used to run it. - - The "Corresponding Source" for a work in object code form means all -the source code needed to generate, install, and (for an executable -work) run the object code and to modify the work, including scripts to -control those activities. However, it does not include the work's -System Libraries, or general-purpose tools or generally available free -programs which are used unmodified in performing those activities but -which are not part of the work. For example, Corresponding Source -includes interface definition files associated with source files for -the work, and the source code for shared libraries and dynamically -linked subprograms that the work is specifically designed to require, -such as by intimate data communication or control flow between those -subprograms and other parts of the work. - - The Corresponding Source need not include anything that users -can regenerate automatically from other parts of the Corresponding -Source. - - The Corresponding Source for a work in source code form is that -same work. - - 2. Basic Permissions. - - All rights granted under this License are granted for the term of -copyright on the Program, and are irrevocable provided the stated -conditions are met. This License explicitly affirms your unlimited -permission to run the unmodified Program. The output from running a -covered work is covered by this License only if the output, given its -content, constitutes a covered work. This License acknowledges your -rights of fair use or other equivalent, as provided by copyright law. - - You may make, run and propagate covered works that you do not -convey, without conditions so long as your license otherwise remains -in force. You may convey covered works to others for the sole purpose -of having them make modifications exclusively for you, or provide you -with facilities for running those works, provided that you comply with -the terms of this License in conveying all material for which you do -not control copyright. Those thus making or running the covered works -for you must do so exclusively on your behalf, under your direction -and control, on terms that prohibit them from making any copies of -your copyrighted material outside their relationship with you. - - Conveying under any other circumstances is permitted solely under -the conditions stated below. Sublicensing is not allowed; section 10 -makes it unnecessary. - - 3. Protecting Users' Legal Rights From Anti-Circumvention Law. - - No covered work shall be deemed part of an effective technological -measure under any applicable law fulfilling obligations under article -11 of the WIPO copyright treaty adopted on 20 December 1996, or -similar laws prohibiting or restricting circumvention of such -measures. - - When you convey a covered work, you waive any legal power to forbid -circumvention of technological measures to the extent such circumvention -is effected by exercising rights under this License with respect to -the covered work, and you disclaim any intention to limit operation or -modification of the work as a means of enforcing, against the work's -users, your or third parties' legal rights to forbid circumvention of -technological measures. - - 4. Conveying Verbatim Copies. - - You may convey verbatim copies of the Program's source code as you -receive it, in any medium, provided that you conspicuously and -appropriately publish on each copy an appropriate copyright notice; -keep intact all notices stating that this License and any -non-permissive terms added in accord with section 7 apply to the code; -keep intact all notices of the absence of any warranty; and give all -recipients a copy of this License along with the Program. - - You may charge any price or no price for each copy that you convey, -and you may offer support or warranty protection for a fee. - - 5. Conveying Modified Source Versions. - - You may convey a work based on the Program, or the modifications to -produce it from the Program, in the form of source code under the -terms of section 4, provided that you also meet all of these conditions: - - a) The work must carry prominent notices stating that you modified - it, and giving a relevant date. - - b) The work must carry prominent notices stating that it is - released under this License and any conditions added under section - 7. This requirement modifies the requirement in section 4 to - "keep intact all notices". - - c) You must license the entire work, as a whole, under this - License to anyone who comes into possession of a copy. This - License will therefore apply, along with any applicable section 7 - additional terms, to the whole of the work, and all its parts, - regardless of how they are packaged. This License gives no - permission to license the work in any other way, but it does not - invalidate such permission if you have separately received it. - - d) If the work has interactive user interfaces, each must display - Appropriate Legal Notices; however, if the Program has interactive - interfaces that do not display Appropriate Legal Notices, your - work need not make them do so. - - A compilation of a covered work with other separate and independent -works, which are not by their nature extensions of the covered work, -and which are not combined with it such as to form a larger program, -in or on a volume of a storage or distribution medium, is called an -"aggregate" if the compilation and its resulting copyright are not -used to limit the access or legal rights of the compilation's users -beyond what the individual works permit. Inclusion of a covered work -in an aggregate does not cause this License to apply to the other -parts of the aggregate. - - 6. Conveying Non-Source Forms. - - You may convey a covered work in object code form under the terms -of sections 4 and 5, provided that you also convey the -machine-readable Corresponding Source under the terms of this License, -in one of these ways: - - a) Convey the object code in, or embodied in, a physical product - (including a physical distribution medium), accompanied by the - Corresponding Source fixed on a durable physical medium - customarily used for software interchange. - - b) Convey the object code in, or embodied in, a physical product - (including a physical distribution medium), accompanied by a - written offer, valid for at least three years and valid for as - long as you offer spare parts or customer support for that product - model, to give anyone who possesses the object code either (1) a - copy of the Corresponding Source for all the software in the - product that is covered by this License, on a durable physical - medium customarily used for software interchange, for a price no - more than your reasonable cost of physically performing this - conveying of source, or (2) access to copy the - Corresponding Source from a network server at no charge. - - c) Convey individual copies of the object code with a copy of the - written offer to provide the Corresponding Source. This - alternative is allowed only occasionally and noncommercially, and - only if you received the object code with such an offer, in accord - with subsection 6b. - - d) Convey the object code by offering access from a designated - place (gratis or for a charge), and offer equivalent access to the - Corresponding Source in the same way through the same place at no - further charge. You need not require recipients to copy the - Corresponding Source along with the object code. If the place to - copy the object code is a network server, the Corresponding Source - may be on a different server (operated by you or a third party) - that supports equivalent copying facilities, provided you maintain - clear directions next to the object code saying where to find the - Corresponding Source. Regardless of what server hosts the - Corresponding Source, you remain obligated to ensure that it is - available for as long as needed to satisfy these requirements. - - e) Convey the object code using peer-to-peer transmission, provided - you inform other peers where the object code and Corresponding - Source of the work are being offered to the general public at no - charge under subsection 6d. - - A separable portion of the object code, whose source code is excluded -from the Corresponding Source as a System Library, need not be -included in conveying the object code work. - - A "User Product" is either (1) a "consumer product", which means any -tangible personal property which is normally used for personal, family, -or household purposes, or (2) anything designed or sold for incorporation -into a dwelling. In determining whether a product is a consumer product, -doubtful cases shall be resolved in favor of coverage. For a particular -product received by a particular user, "normally used" refers to a -typical or common use of that class of product, regardless of the status -of the particular user or of the way in which the particular user -actually uses, or expects or is expected to use, the product. A product -is a consumer product regardless of whether the product has substantial -commercial, industrial or non-consumer uses, unless such uses represent -the only significant mode of use of the product. - - "Installation Information" for a User Product means any methods, -procedures, authorization keys, or other information required to install -and execute modified versions of a covered work in that User Product from -a modified version of its Corresponding Source. The information must -suffice to ensure that the continued functioning of the modified object -code is in no case prevented or interfered with solely because -modification has been made. - - If you convey an object code work under this section in, or with, or -specifically for use in, a User Product, and the conveying occurs as -part of a transaction in which the right of possession and use of the -User Product is transferred to the recipient in perpetuity or for a -fixed term (regardless of how the transaction is characterized), the -Corresponding Source conveyed under this section must be accompanied -by the Installation Information. But this requirement does not apply -if neither you nor any third party retains the ability to install -modified object code on the User Product (for example, the work has -been installed in ROM). - - The requirement to provide Installation Information does not include a -requirement to continue to provide support service, warranty, or updates -for a work that has been modified or installed by the recipient, or for -the User Product in which it has been modified or installed. Access to a -network may be denied when the modification itself materially and -adversely affects the operation of the network or violates the rules and -protocols for communication across the network. - - Corresponding Source conveyed, and Installation Information provided, -in accord with this section must be in a format that is publicly -documented (and with an implementation available to the public in -source code form), and must require no special password or key for -unpacking, reading or copying. - - 7. Additional Terms. - - "Additional permissions" are terms that supplement the terms of this -License by making exceptions from one or more of its conditions. -Additional permissions that are applicable to the entire Program shall -be treated as though they were included in this License, to the extent -that they are valid under applicable law. If additional permissions -apply only to part of the Program, that part may be used separately -under those permissions, but the entire Program remains governed by -this License without regard to the additional permissions. - - When you convey a copy of a covered work, you may at your option -remove any additional permissions from that copy, or from any part of -it. (Additional permissions may be written to require their own -removal in certain cases when you modify the work.) You may place -additional permissions on material, added by you to a covered work, -for which you have or can give appropriate copyright permission. - - Notwithstanding any other provision of this License, for material you -add to a covered work, you may (if authorized by the copyright holders of -that material) supplement the terms of this License with terms: - - a) Disclaiming warranty or limiting liability differently from the - terms of sections 15 and 16 of this License; or - - b) Requiring preservation of specified reasonable legal notices or - author attributions in that material or in the Appropriate Legal - Notices displayed by works containing it; or - - c) Prohibiting misrepresentation of the origin of that material, or - requiring that modified versions of such material be marked in - reasonable ways as different from the original version; or - - d) Limiting the use for publicity purposes of names of licensors or - authors of the material; or - - e) Declining to grant rights under trademark law for use of some - trade names, trademarks, or service marks; or - - f) Requiring indemnification of licensors and authors of that - material by anyone who conveys the material (or modified versions of - it) with contractual assumptions of liability to the recipient, for - any liability that these contractual assumptions directly impose on - those licensors and authors. - - All other non-permissive additional terms are considered "further -restrictions" within the meaning of section 10. If the Program as you -received it, or any part of it, contains a notice stating that it is -governed by this License along with a term that is a further -restriction, you may remove that term. If a license document contains -a further restriction but permits relicensing or conveying under this -License, you may add to a covered work material governed by the terms -of that license document, provided that the further restriction does -not survive such relicensing or conveying. - - If you add terms to a covered work in accord with this section, you -must place, in the relevant source files, a statement of the -additional terms that apply to those files, or a notice indicating -where to find the applicable terms. - - Additional terms, permissive or non-permissive, may be stated in the -form of a separately written license, or stated as exceptions; -the above requirements apply either way. - - 8. Termination. - - You may not propagate or modify a covered work except as expressly -provided under this License. Any attempt otherwise to propagate or -modify it is void, and will automatically terminate your rights under -this License (including any patent licenses granted under the third -paragraph of section 11). - - However, if you cease all violation of this License, then your -license from a particular copyright holder is reinstated (a) -provisionally, unless and until the copyright holder explicitly and -finally terminates your license, and (b) permanently, if the copyright -holder fails to notify you of the violation by some reasonable means -prior to 60 days after the cessation. - - Moreover, your license from a particular copyright holder is -reinstated permanently if the copyright holder notifies you of the -violation by some reasonable means, this is the first time you have -received notice of violation of this License (for any work) from that -copyright holder, and you cure the violation prior to 30 days after -your receipt of the notice. - - Termination of your rights under this section does not terminate the -licenses of parties who have received copies or rights from you under -this License. If your rights have been terminated and not permanently -reinstated, you do not qualify to receive new licenses for the same -material under section 10. - - 9. Acceptance Not Required for Having Copies. - - You are not required to accept this License in order to receive or -run a copy of the Program. Ancillary propagation of a covered work -occurring solely as a consequence of using peer-to-peer transmission -to receive a copy likewise does not require acceptance. However, -nothing other than this License grants you permission to propagate or -modify any covered work. These actions infringe copyright if you do -not accept this License. Therefore, by modifying or propagating a -covered work, you indicate your acceptance of this License to do so. - - 10. Automatic Licensing of Downstream Recipients. - - Each time you convey a covered work, the recipient automatically -receives a license from the original licensors, to run, modify and -propagate that work, subject to this License. You are not responsible -for enforcing compliance by third parties with this License. - - An "entity transaction" is a transaction transferring control of an -organization, or substantially all assets of one, or subdividing an -organization, or merging organizations. If propagation of a covered -work results from an entity transaction, each party to that -transaction who receives a copy of the work also receives whatever -licenses to the work the party's predecessor in interest had or could -give under the previous paragraph, plus a right to possession of the -Corresponding Source of the work from the predecessor in interest, if -the predecessor has it or can get it with reasonable efforts. - - You may not impose any further restrictions on the exercise of the -rights granted or affirmed under this License. For example, you may -not impose a license fee, royalty, or other charge for exercise of -rights granted under this License, and you may not initiate litigation -(including a cross-claim or counterclaim in a lawsuit) alleging that -any patent claim is infringed by making, using, selling, offering for -sale, or importing the Program or any portion of it. - - 11. Patents. - - A "contributor" is a copyright holder who authorizes use under this -License of the Program or a work on which the Program is based. The -work thus licensed is called the contributor's "contributor version". - - A contributor's "essential patent claims" are all patent claims -owned or controlled by the contributor, whether already acquired or -hereafter acquired, that would be infringed by some manner, permitted -by this License, of making, using, or selling its contributor version, -but do not include claims that would be infringed only as a -consequence of further modification of the contributor version. For -purposes of this definition, "control" includes the right to grant -patent sublicenses in a manner consistent with the requirements of -this License. - - Each contributor grants you a non-exclusive, worldwide, royalty-free -patent license under the contributor's essential patent claims, to -make, use, sell, offer for sale, import and otherwise run, modify and -propagate the contents of its contributor version. - - In the following three paragraphs, a "patent license" is any express -agreement or commitment, however denominated, not to enforce a patent -(such as an express permission to practice a patent or covenant not to -sue for patent infringement). To "grant" such a patent license to a -party means to make such an agreement or commitment not to enforce a -patent against the party. - - If you convey a covered work, knowingly relying on a patent license, -and the Corresponding Source of the work is not available for anyone -to copy, free of charge and under the terms of this License, through a -publicly available network server or other readily accessible means, -then you must either (1) cause the Corresponding Source to be so -available, or (2) arrange to deprive yourself of the benefit of the -patent license for this particular work, or (3) arrange, in a manner -consistent with the requirements of this License, to extend the patent -license to downstream recipients. "Knowingly relying" means you have -actual knowledge that, but for the patent license, your conveying the -covered work in a country, or your recipient's use of the covered work -in a country, would infringe one or more identifiable patents in that -country that you have reason to believe are valid. - - If, pursuant to or in connection with a single transaction or -arrangement, you convey, or propagate by procuring conveyance of, a -covered work, and grant a patent license to some of the parties -receiving the covered work authorizing them to use, propagate, modify -or convey a specific copy of the covered work, then the patent license -you grant is automatically extended to all recipients of the covered -work and works based on it. - - A patent license is "discriminatory" if it does not include within -the scope of its coverage, prohibits the exercise of, or is -conditioned on the non-exercise of one or more of the rights that are -specifically granted under this License. You may not convey a covered -work if you are a party to an arrangement with a third party that is -in the business of distributing software, under which you make payment -to the third party based on the extent of your activity of conveying -the work, and under which the third party grants, to any of the -parties who would receive the covered work from you, a discriminatory -patent license (a) in connection with copies of the covered work -conveyed by you (or copies made from those copies), or (b) primarily -for and in connection with specific products or compilations that -contain the covered work, unless you entered into that arrangement, -or that patent license was granted, prior to 28 March 2007. - - Nothing in this License shall be construed as excluding or limiting -any implied license or other defenses to infringement that may -otherwise be available to you under applicable patent law. - - 12. No Surrender of Others' Freedom. - - If conditions are imposed on you (whether by court order, agreement or -otherwise) that contradict the conditions of this License, they do not -excuse you from the conditions of this License. If you cannot convey a -covered work so as to satisfy simultaneously your obligations under this -License and any other pertinent obligations, then as a consequence you may -not convey it at all. For example, if you agree to terms that obligate you -to collect a royalty for further conveying from those to whom you convey -the Program, the only way you could satisfy both those terms and this -License would be to refrain entirely from conveying the Program. - - 13. Use with the GNU Affero General Public License. - - Notwithstanding any other provision of this License, you have -permission to link or combine any covered work with a work licensed -under version 3 of the GNU Affero General Public License into a single -combined work, and to convey the resulting work. The terms of this -License will continue to apply to the part which is the covered work, -but the special requirements of the GNU Affero General Public License, -section 13, concerning interaction through a network will apply to the -combination as such. - - 14. Revised Versions of this License. - - The Free Software Foundation may publish revised and/or new versions of -the GNU General Public License from time to time. Such new versions will -be similar in spirit to the present version, but may differ in detail to -address new problems or concerns. - - Each version is given a distinguishing version number. If the -Program specifies that a certain numbered version of the GNU General -Public License "or any later version" applies to it, you have the -option of following the terms and conditions either of that numbered -version or of any later version published by the Free Software -Foundation. If the Program does not specify a version number of the -GNU General Public License, you may choose any version ever published -by the Free Software Foundation. - - If the Program specifies that a proxy can decide which future -versions of the GNU General Public License can be used, that proxy's -public statement of acceptance of a version permanently authorizes you -to choose that version for the Program. - - Later license versions may give you additional or different -permissions. However, no additional obligations are imposed on any -author or copyright holder as a result of your choosing to follow a -later version. - - 15. Disclaimer of Warranty. - - THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY -APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT -HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY -OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, -THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR -PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM -IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF -ALL NECESSARY SERVICING, REPAIR OR CORRECTION. - - 16. Limitation of Liability. - - IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING -WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS -THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY -GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE -USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF -DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD -PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), -EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF -SUCH DAMAGES. - - 17. Interpretation of Sections 15 and 16. - - If the disclaimer of warranty and limitation of liability provided -above cannot be given local legal effect according to their terms, -reviewing courts shall apply local law that most closely approximates -an absolute waiver of all civil liability in connection with the -Program, unless a warranty or assumption of liability accompanies a -copy of the Program in return for a fee. - - END OF TERMS AND CONDITIONS - - How to Apply These Terms to Your New Programs - - If you develop a new program, and you want it to be of the greatest -possible use to the public, the best way to achieve this is to make it -free software which everyone can redistribute and change under these terms. - - To do so, attach the following notices to the program. It is safest -to attach them to the start of each source file to most effectively -state the exclusion of warranty; and each file should have at least -the "copyright" line and a pointer to where the full notice is found. - - - Copyright (C) - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . - -Also add information on how to contact you by electronic and paper mail. - - If the program does terminal interaction, make it output a short -notice like this when it starts in an interactive mode: - - Copyright (C) - This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. - This is free software, and you are welcome to redistribute it - under certain conditions; type `show c' for details. - -The hypothetical commands `show w' and `show c' should show the appropriate -parts of the General Public License. Of course, your program's commands -might be different; for a GUI interface, you would use an "about box". - - You should also get your employer (if you work as a programmer) or school, -if any, to sign a "copyright disclaimer" for the program, if necessary. -For more information on this, and how to apply and follow the GNU GPL, see -. - - The GNU General Public License does not permit incorporating your program -into proprietary programs. If your program is a subroutine library, you -may consider it more useful to permit linking proprietary applications with -the library. If this is what you want to do, use the GNU Lesser General -Public License instead of this License. But first, please read -. \ No newline at end of file +YEAR: 2023 +COPYRIGHT HOLDER: datawizard authors diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 000000000..e8e0fe91d --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,21 @@ +# MIT License + +Copyright (c) 2023 datawizard authors + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/NEWS.md b/NEWS.md index 858b95f93..9f479a117 100644 --- a/NEWS.md +++ b/NEWS.md @@ -13,6 +13,8 @@ CHANGES * `recode_into()` gains an `overwrite` argument to skip overwriting already recoded cases when multiple recode patterns apply to the same case. +* `datawizard` moves from the GPL-3 license to the MIT license. + BUG FIXES * Fixed issues in `data_write()` when writing labelled data into SPSS format