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

Deprecate pattern in favour of select in data_rename() #568

Merged
merged 20 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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.13.0.15
Version: 0.13.0.16
Authors@R: c(
person("Indrajeet", "Patil", , "[email protected]", role = "aut",
comment = c(ORCID = "0000-0003-1995-6531")),
Expand Down
9 changes: 6 additions & 3 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# datawizard (development)

BREAKING CHANGES
DEPRECATIONS

* Argument `drop_na` in `data_match()` is deprecated now. Please use
`remove_na` instead.

* Argument `drop_na` in `data_match()` is deprecated now. Please use `remove_na`
instead.
* Argument `pattern` in `data_rename()` is deprecated. Please use `select`
instead (#567).

CHANGES

Expand Down
75 changes: 41 additions & 34 deletions R/data_rename.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@
#' pipe-workflow.
#'
#' @param data A data frame, or an object that can be coerced to a data frame.
#' @param pattern Character vector.
#' @param select Character vector.
#' - For `data_addprefix()` or `data_addsuffix()`, a character string, which
#' will be added as prefix or suffix to the column names.
etiennebacher marked this conversation as resolved.
Show resolved Hide resolved
#' - For `data_rename()`, indicates columns that should be selected for
#' renaming. Can be `NULL` (in which case all columns are selected).
#' `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
#' `select` can also be a named vector. In this case, names are used as
#' values for the `replacement` argument (i.e. `select` can be a character
#' vector using `<new name> = "<old name>"` and argument `replacement` will
#' be ignored then).
#' @param replacement Character vector. Can be one of the following:
#' - A character vector that indicates the new names of the columns selected
#' in `pattern`. `pattern` and `replacement` must be of the same length.
#' in `select`. `select` and `replacement` must be of the same length.
#' - `NULL`, in which case columns are numbered in sequential order.
#' - A string (i.e. character vector of length 1) with a "glue" styled pattern.
#' Currently supported tokens are:
#' - `{col}` which will be replaced by the column name, i.e. the
#' corresponding value in `pattern`.
#' corresponding value in `select`.
#' - `{n}` will be replaced by the number of the variable that is replaced.
#' - `{letter}` will be replaced by alphabetical letters in sequential order.
#' If more than 26 letters are required, letters are repeated, but have
Expand All @@ -39,18 +39,19 @@
#' ```r
#' data_rename(
#' mtcars,
#' pattern = c("am", "vs"),
#' select = c("am", "vs"),
#' replacement = "new_name_from_{col}"
#' )
#' ```
#' ... which would return new column names `new_name_from_am` and
#' `new_name_from_vs`. See 'Examples'.
#'
#' If `pattern` is a named vector, `replacement` is ignored.
#' If `select` is a named vector, `replacement` is ignored.
#' @param rows Vector of row names.
#' @param safe Do not throw error if for instance the variable to be
#' renamed/removed doesn't exist.
#' @param verbose Toggle warnings and messages.
#' @param pattern Deprecated. Use `select` instead.
#' @param ... Other arguments passed to or from other functions.
etiennebacher marked this conversation as resolved.
Show resolved Hide resolved
#'
#' @return A modified data frame.
Expand Down Expand Up @@ -96,28 +97,34 @@
#'
#' @export
data_rename <- function(data,
pattern = NULL,
select = NULL,
replacement = NULL,
safe = TRUE,
verbose = TRUE,
pattern = NULL,
...) {
if (!is.null(pattern)) {
.is_deprecated("pattern", "select")
select <- pattern
}

# change all names if no pattern specified
if (is.null(pattern)) {
pattern <- names(data)
if (is.null(select)) {
select <- names(data)
}
strengejacke marked this conversation as resolved.
Show resolved Hide resolved

if (!is.character(pattern)) {
insight::format_error("Argument `pattern` must be of type character.")
if (!is.character(select)) {
insight::format_error("Argument `select` must be of type character.")
}
etiennebacher marked this conversation as resolved.
Show resolved Hide resolved

# check if `pattern` has names, and if so, use as "replacement"
if (!is.null(names(pattern))) {
replacement <- names(pattern)
# check if `select` has names, and if so, use as "replacement"
if (!is.null(names(select))) {
replacement <- names(select)
}

# name columns 1, 2, 3 etc. if no replacement
if (is.null(replacement)) {
replacement <- paste0(seq_along(pattern))
replacement <- paste0(seq_along(select))
}

# coerce to character
Expand All @@ -126,22 +133,22 @@ data_rename <- function(data,
# check if `replacement` has no empty strings and no NA values
invalid_replacement <- is.na(replacement) | !nzchar(replacement)
if (any(invalid_replacement)) {
if (is.null(names(pattern))) {
# when user did not match `pattern` with `replacement`
if (is.null(names(select))) {
# when user did not match `select` with `replacement`
msg <- c(
"`replacement` is not allowed to have `NA` or empty strings.",
sprintf(
"Following values in `pattern` have no match in `replacement`: %s",
toString(pattern[invalid_replacement])
"Following values in `select` have no match in `replacement`: %s",
toString(select[invalid_replacement])
)
)
} else {
# when user did not name all elements of `pattern`
# when user did not name all elements of `select`
msg <- c(
"Either name all elements of `pattern` or use `replacement`.",
"Either name all elements of `select` or use `replacement`.",
sprintf(
"Following values in `pattern` were not named: %s",
toString(pattern[invalid_replacement])
"Following values in `select` were not named: %s",
toString(select[invalid_replacement])
)
)
}
Expand All @@ -163,30 +170,30 @@ data_rename <- function(data,
# check if we have "glue" styled replacement-string
glue_style <- length(replacement) == 1 && grepl("{", replacement, fixed = TRUE)

if (length(replacement) > length(pattern) && verbose) {
if (length(replacement) > length(select) && verbose) {
insight::format_alert(
paste0(
"There are more names in `replacement` than in `pattern`. The last ",
length(replacement) - length(pattern), " names of `replacement` are not used."
"There are more names in `replacement` than in `select`. The last ",
length(replacement) - length(select), " names of `replacement` are not used."
)
)
} else if (length(replacement) < length(pattern) && verbose && !glue_style) {
} else if (length(replacement) < length(select) && verbose && !glue_style) {
insight::format_alert(
paste0(
"There are more names in `pattern` than in `replacement`. The last ",
length(pattern) - length(replacement), " names of `pattern` are not modified."
"There are more names in `select` than in `replacement`. The last ",
length(select) - length(replacement), " names of `select` are not modified."
)
)
}

# if we have glue-styled replacement-string, create replacement pattern now
# if we have glue-styled replacement-string, create replacement select now
if (glue_style) {
replacement <- .glue_replacement(pattern, replacement)
replacement <- .glue_replacement(select, replacement)
}

for (i in seq_along(pattern)) {
for (i in seq_along(select)) {
if (!is.na(replacement[i])) {
data <- .data_rename(data, pattern[i], replacement[i], safe, verbose)
data <- .data_rename(data, select[i], replacement[i], safe, verbose)
}
}

Expand Down
10 changes: 9 additions & 1 deletion R/text_format.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@
#' @param width Positive integer giving the target column width for wrapping
#' lines in the output. Can be "auto", in which case it will select 90\% of the
#' default width.
#' @inheritParams data_rename
#' @param pattern Character vector.
#' - For `data_addprefix()` or `data_addsuffix()`, a character string, which
#' will be added as prefix or suffix to the column names.
#' - For `data_rename()`, indicates columns that should be selected for
etiennebacher marked this conversation as resolved.
Show resolved Hide resolved
#' renaming. Can be `NULL` (in which case all columns are selected).
#' `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>"` and argument `replacement` will
#' be ignored then).
#' @param sep Separator.
#' @param last Last separator.
#' @param n The number of characters to find.
Expand Down
13 changes: 4 additions & 9 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ CMD
Carle
Catran
Crosstables
Dhaliwal
Disaggregating
DEPRECATIONS
DOI
De
Dom
Dhaliwal
Disaggregating
EFC
Enders
EUROFAMCARE
Enders
Fairbrother
GLMM
Gelman
Expand Down Expand Up @@ -54,7 +54,6 @@ Winsorizing
al
behaviour
behaviours
bmwiernik
codebook
codebooks
coercible
Expand All @@ -77,7 +76,6 @@ joss
labelled
labelling
leptokurtic
lifecycle
lm
lme
meaned
Expand All @@ -88,7 +86,6 @@ modelling
nd
panelr
partialization
patilindrajeets
platykurtic
poorman
pre
Expand All @@ -102,7 +99,6 @@ recodes
recoding
recodings
relevel
rempsyc
reproducibility
rescale
rescaled
Expand All @@ -111,7 +107,6 @@ rio
rowid
sd
stackexchange
strengejacke
tailedness
th
tibble
Expand Down
54 changes: 11 additions & 43 deletions man/data_rename.Rd

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

2 changes: 1 addition & 1 deletion tests/testthat/test-attributes.R
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ test_that("convert_to_na, attributes preserved", {
test_that("data_rename, attributes preserved", {
x <- mtcars
attr(x, "myattri") <- "I'm here"
x2 <- data_rename(x, pattern = "hp", replacement = "horsepower")
x2 <- data_rename(x, select = "hp", replacement = "horsepower")
expect_identical(attr(x2, "myattri", exact = TRUE), "I'm here")
})

Expand Down
Loading
Loading