Skip to content

Commit

Permalink
data_ranem() works with named vector
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Sep 8, 2024
1 parent e1086a4 commit 3a07706
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 6 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: datawizard
Title: Easy Data Wrangling and Statistical Transformations
Version: 0.12.3.1
Version: 0.12.3.2
Authors@R: c(
person("Indrajeet", "Patil", , "[email protected]", role = "aut",
comment = c(ORCID = "0000-0003-1995-6531", Twitter = "@patilindrajeets")),
Expand Down
13 changes: 12 additions & 1 deletion R/data_rename.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<new name> = <old name>`).
#' @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
Expand All @@ -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))
#'
Expand Down Expand Up @@ -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))
Expand Down
7 changes: 6 additions & 1 deletion man/data_rename.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion man/text_format.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 10 additions & 2 deletions tests/testthat/test-data_rename.R
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand Down Expand Up @@ -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))
Expand All @@ -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))
Expand Down

0 comments on commit 3a07706

Please sign in to comment.