From 0d30d24c98457ff885942c2f46c6d223ed74368b Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 8 Sep 2024 16:59:05 +0200 Subject: [PATCH] data_rename() works with named vector (#538) * data_ranem() works with named vector * news --- DESCRIPTION | 2 +- NEWS.md | 12 ++++++++++-- R/data_rename.R | 13 ++++++++++++- man/data_rename.Rd | 8 +++++++- man/text_format.Rd | 5 ++++- tests/testthat/test-data_rename.R | 12 ++++++++++-- 6 files changed, 44 insertions(+), 8 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 5d46bf056..b453e2abf 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: datawizard Title: Easy Data Wrangling and Statistical Transformations -Version: 0.12.3.1 +Version: 0.12.3.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 50bb72db6..f7575f524 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,14 +1,22 @@ +# datawizard (development) + +CHANGES + +* The `pattern` argument in `data_rename()` can also be a named vector. In this + case, names are used as values for the `replacement` argument (i.e. `pattern` + can be a character vector using ` = ""`). + # datawizard 0.12.3 CHANGES * `demean()` (and `degroup()`) now also work for nested designs, if argument `nested = TRUE` and `by` specifies more than one variable (#533). - + * Vignettes are no longer provided in the package, they are now only available on the website. There is only one "Overview" vignette available in the package, it contains links to the other vignettes on the website. This is because there - are CRAN errors occurring when building vignettes on macOS and we couldn't + are CRAN errors occurring when building vignettes on macOS and we couldn't determine the cause after multiple patch releases (#534). # datawizard 0.12.2 diff --git a/R/data_rename.R b/R/data_rename.R index b8f213c7f..8b5f5c9ca 100644 --- a/R/data_rename.R +++ b/R/data_rename.R @@ -13,7 +13,10 @@ #' @param pattern Character vector. For `data_rename()`, indicates columns that #' should be selected for renaming. Can be `NULL` (in which case all columns #' are selected). For `data_addprefix()` or `data_addsuffix()`, a character -#' string, which will be added as prefix or suffix to the column names. +#' string, which will be added as prefix or suffix to the column names. For +#' `data_rename()`, `pattern` can also be a named vector. In this case, names +#' are used as values for the `replacement` argument (i.e. `pattern` can be a +#' character vector using ` = ""`). #' @param replacement Character vector. Indicates the new name of the columns #' selected in `pattern`. Can be `NULL` (in which case column are numbered #' in sequential order). If not `NULL`, `pattern` and `replacement` must be @@ -33,6 +36,9 @@ #' head(data_rename(iris, "FakeCol", "length")) # This doesn't #' head(data_rename(iris, c("Sepal.Length", "Sepal.Width"), c("length", "width"))) #' +#' # use named vector to rename +#' head(data_rename(iris, c(length = "Sepal.Length", width = "Sepal.Width"))) +#' #' # Reset names #' head(data_rename(iris, NULL)) #' @@ -66,6 +72,11 @@ data_rename <- function(data, insight::format_error("Argument `pattern` must be of type character.") } + # check if `pattern` has names, and if so, use as "replacement" + if (!is.null(names(pattern))) { + replacement <- names(pattern) + } + # name columns 1, 2, 3 etc. if no replacement if (is.null(replacement)) { replacement <- paste0(seq_along(pattern)) diff --git a/man/data_rename.Rd b/man/data_rename.Rd index f1f4de938..77bd95f65 100644 --- a/man/data_rename.Rd +++ b/man/data_rename.Rd @@ -46,7 +46,10 @@ data_rename_rows(data, rows = NULL) \item{pattern}{Character vector. For \code{data_rename()}, indicates columns that should be selected for renaming. Can be \code{NULL} (in which case all columns are selected). For \code{data_addprefix()} or \code{data_addsuffix()}, a character -string, which will be added as prefix or suffix to the column names.} +string, which will be added as prefix or suffix to the column names. For +\code{data_rename()}, \code{pattern} can also be a named vector. In this case, names +are used as values for the \code{replacement} argument (i.e. \code{pattern} can be a +character vector using \verb{ = ""}).} \item{select}{Variables that will be included when performing the required tasks. Can be either @@ -134,6 +137,9 @@ head(data_rename(iris, "Sepal.Length", "length")) head(data_rename(iris, "FakeCol", "length")) # This doesn't head(data_rename(iris, c("Sepal.Length", "Sepal.Width"), c("length", "width"))) +# use named vector to rename +head(data_rename(iris, c(length = "Sepal.Length", width = "Sepal.Width"))) + # Reset names head(data_rename(iris, NULL)) diff --git a/man/text_format.Rd b/man/text_format.Rd index 87f045193..3ce77210c 100644 --- a/man/text_format.Rd +++ b/man/text_format.Rd @@ -63,7 +63,10 @@ text elements will not be enclosed.} \item{pattern}{Character vector. For \code{data_rename()}, indicates columns that should be selected for renaming. Can be \code{NULL} (in which case all columns are selected). For \code{data_addprefix()} or \code{data_addsuffix()}, a character -string, which will be added as prefix or suffix to the column names.} +string, which will be added as prefix or suffix to the column names. For +\code{data_rename()}, \code{pattern} can also be a named vector. In this case, names +are used as values for the \code{replacement} argument (i.e. \code{pattern} can be a +character vector using \verb{ = ""}).} } \value{ A character string. diff --git a/tests/testthat/test-data_rename.R b/tests/testthat/test-data_rename.R index a8d003b59..db054a23d 100644 --- a/tests/testthat/test-data_rename.R +++ b/tests/testthat/test-data_rename.R @@ -14,6 +14,10 @@ test_that("data_rename works with one or several replacements", { ), c("length", "width", "Petal.Length", "Petal.Width", "Species") ) + expect_named( + data_rename(test, c(length = "Sepal.Length", width = "Sepal.Width")), + c("length", "width", "Petal.Length", "Petal.Width", "Species") + ) }) test_that("data_rename returns a data frame", { @@ -42,7 +46,9 @@ test_that("data_rename uses indices when no replacement", { test_that("data_rename works when too many names in 'replacement'", { expect_message( - x <- data_rename(test, replacement = paste0("foo", 1:6)), + { + x <- data_rename(test, replacement = paste0("foo", 1:6)) + }, "There are more names in" ) expect_identical(dim(test), dim(x)) @@ -51,7 +57,9 @@ test_that("data_rename works when too many names in 'replacement'", { test_that("data_rename works when not enough names in 'replacement'", { expect_message( - x <- data_rename(test, replacement = paste0("foo", 1:2)), + { + x <- data_rename(test, replacement = paste0("foo", 1:2)) + }, "There are more names in" ) expect_identical(dim(test), dim(x))