Skip to content

Commit

Permalink
data_rename() works with named vector (#538)
Browse files Browse the repository at this point in the history
* data_ranem() works with named vector

* news
  • Loading branch information
strengejacke authored Sep 8, 2024
1 parent e1086a4 commit 0d30d24
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 8 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
12 changes: 10 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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 `<new name> = "<old name>"`).

# 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
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 using `<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
8 changes: 7 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 0d30d24

Please sign in to comment.