Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

data_rename() works with named vector #538

Merged
merged 2 commits into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 All @@ -45,7 +51,7 @@
#' - Functions to reshape, pivot or rotate data frames: [data_to_long()], [data_to_wide()], [data_rotate()]
#' - Functions to recode data: [rescale()], [reverse()], [categorize()],
#' [recode_values()], [slide()]
#' - Functions to standardize, normalize, rank-transform: [center()], [standardize()], [normalize()], [ranktransform()], [winsorize()]

Check warning on line 54 in R/data_rename.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/data_rename.R,line=54,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 134 characters.

Check warning on line 54 in R/data_rename.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/data_rename.R,line=54,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 134 characters.
#' - Split and merge data frames: [data_partition()], [data_merge()]
#' - Functions to find or select columns: [data_select()], [extract_column_names()]
#' - Functions to filter rows: [data_match()], [data_filter()]
Expand All @@ -66,6 +72,11 @@
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")
)
Comment on lines +17 to +20
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, can you add a test about what happens when someone passes a partially named vector, e.g. data_rename(test, c(length = "Sepal.Length", "Sepal.Width"))? I suppose it would error but I just want to ensure it throws a nice error message.

Same thing for the case where the user passes a named vector and a replacement, e.g. data_rename(test, c(length = "Sepal.Length"), replacement = "Sepal.Width")

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First example will remove column name, second will ignore replacement. First example is in line with the current behaviour:
data_rename(iris, c("Sepal.Length", "Sepal.Width"), replacement = c("aaa", ""))

For the second case, I have made this more explicit in the docs.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #539

})

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
Loading