From 9c8fcc0d8e3f361109bbe61d082463a1e36972c5 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 24 Nov 2024 22:09:27 +0100 Subject: [PATCH 1/8] minor docs --- R/data_summary.R | 6 +++--- man/data_summary.Rd | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/R/data_summary.R b/R/data_summary.R index 7662d0c94..546c47659 100644 --- a/R/data_summary.R +++ b/R/data_summary.R @@ -5,9 +5,9 @@ #' data frame or a matrix. #' #' @param x A (grouped) data frame. -#' @param by Optional character string, indicating the name of a variable in `x`. -#' If supplied, the data will be split by this variable and summary statistics -#' will be computed for each group. +#' @param by Optional character string, indicating the names of one or more +#' variables in the data frame. If supplied, the data will be split by these +#' variables and summary statistics will be computed for each group. #' @param remove_na Logical. If `TRUE`, missing values are omitted from the #' grouping variable. If `FALSE` (default), missing values are included as a #' level in the grouping variable. diff --git a/man/data_summary.Rd b/man/data_summary.Rd index 24cfa1a9f..82d154175 100644 --- a/man/data_summary.Rd +++ b/man/data_summary.Rd @@ -18,9 +18,9 @@ and the function to compute the summary statistic. Example: as a character string, e.g. \code{"mean_sepal_width = mean(Sepal.Width)"}. The summary function \code{n()} can be used to count the number of observations.} -\item{by}{Optional character string, indicating the name of a variable in \code{x}. -If supplied, the data will be split by this variable and summary statistics -will be computed for each group.} +\item{by}{Optional character string, indicating the names of one or more +variables in the data frame. If supplied, the data will be split by these +variables and summary statistics will be computed for each group.} \item{remove_na}{Logical. If \code{TRUE}, missing values are omitted from the grouping variable. If \code{FALSE} (default), missing values are included as a From b727cb6a729dddd3c98a3949a25ac0f78aebc0fa Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 27 Nov 2024 15:52:10 +0100 Subject: [PATCH 2/8] Allow glue-styled pattern for `data_rename()` (#563) * Allow curl-styled pattern for `data_rename()` * add test * news * lintr * curl -> glue * allow alias * add options * typo * address comments * remove # in comment * fix --- DESCRIPTION | 2 +- NEWS.md | 7 +- R/data_rename.R | 158 ++++++++++++++++++++++++++---- man/categorize.Rd | 12 ++- man/data_match.Rd | 12 ++- man/data_merge.Rd | 12 ++- man/data_partition.Rd | 12 ++- man/data_relocate.Rd | 12 ++- man/data_rename.Rd | 77 ++++++++++++--- man/data_rotate.Rd | 12 ++- man/data_to_long.Rd | 12 ++- man/data_to_wide.Rd | 12 ++- man/extract_column_names.Rd | 12 ++- man/recode_values.Rd | 12 ++- man/slide.Rd | 12 ++- man/text_format.Rd | 19 ++-- man/winsorize.Rd | 12 ++- tests/testthat/test-data_rename.R | 86 ++++++++++++++++ 18 files changed, 401 insertions(+), 92 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 2325c062d..be41e0f6f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: datawizard Title: Easy Data Wrangling and Statistical Transformations -Version: 0.13.0.13 +Version: 0.13.0.14 Authors@R: c( person("Indrajeet", "Patil", , "patilindrajeet.science@gmail.com", role = "aut", comment = c(ORCID = "0000-0003-1995-6531")), diff --git a/NEWS.md b/NEWS.md index 663efa310..15b7f853d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -19,8 +19,11 @@ CHANGES * `data_read()` no longer shows warning about forthcoming breaking changes in upstream packages when reading `.RData` files. -* `data_modify()` now recognizes `n()`, for example to create an index for data groups - with `1:n()` (#535). +* `data_modify()` now recognizes `n()`, for example to create an index for data + groups with `1:n()` (#535). + +* The `replacement` argument in `data_rename()` now supports glue-styled + tokens (#563). BUG FIXES diff --git a/R/data_rename.R b/R/data_rename.R index 18f45657b..f5d6e0e03 100644 --- a/R/data_rename.R +++ b/R/data_rename.R @@ -10,18 +10,43 @@ #' pipe-workflow. #' #' @param data A data frame, or an object that can be coerced to a data frame. -#' @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. 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 ` = ""` and argument `replacement` -#' will be ignored then). -#' @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 -#' of the same length. If `pattern` is a named vector, `replacement` is ignored. +#' @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 +#' 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 ` = ""` 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. +#' - `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`. +#' - `{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 +#' sequential numeric indices (e.g., `a1` to `z1`, followed by `a2` to `z2`). +#' - Finally, the name of a user-defined object that is available in the +#' environment can be used. Note that the object's name is not allowed to +#' be one of the pre-defined tokens, `"col"`, `"n"` and `"letter"`. +#' +#' An example for the use of tokens is... +#' ```r +#' data_rename( +#' mtcars, +#' pattern = 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. #' @param rows Vector of row names. #' @param safe Do not throw error if for instance the variable to be #' renamed/removed doesn't exist. @@ -45,13 +70,26 @@ #' #' # Change all #' head(data_rename(iris, replacement = paste0("Var", 1:5))) +#' +#' # Use glue-styled patterns +#' head(data_rename(mtcars[1:3], c("mpg", "cyl", "disp"), "formerly_{col}")) +#' head(data_rename(mtcars[1:3], c("mpg", "cyl", "disp"), "{col}_is_column_{n}")) +#' head(data_rename(mtcars[1:3], c("mpg", "cyl", "disp"), "new_{letter}")) +#' +#' # User-defined glue-styled patterns from objects in environment +#' x <- c("hi", "there", "!") +#' head(data_rename(mtcars[1:3], c("mpg", "cyl", "disp"), "col_{x}")) #' @seealso -#' - Functions to rename stuff: [data_rename()], [data_rename_rows()], [data_addprefix()], [data_addsuffix()] -#' - Functions to reorder or remove columns: [data_reorder()], [data_relocate()], [data_remove()] -#' - Functions to reshape, pivot or rotate data frames: [data_to_long()], [data_to_wide()], [data_rotate()] +#' - Functions to rename stuff: [data_rename()], [data_rename_rows()], +#' [data_addprefix()], [data_addsuffix()] +#' - Functions to reorder or remove columns: [data_reorder()], [data_relocate()], +#' [data_remove()] +#' - 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()] +#' - Functions to standardize, normalize, rank-transform: [center()], [standardize()], +#' [normalize()], [ranktransform()], [winsorize()] #' - 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()] @@ -122,6 +160,9 @@ 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) { insight::format_alert( paste0( @@ -129,7 +170,7 @@ data_rename <- function(data, length(replacement) - length(pattern), " names of `replacement` are not used." ) ) - } else if (length(replacement) < length(pattern) && verbose) { + } else if (length(replacement) < length(pattern) && verbose && !glue_style) { insight::format_alert( paste0( "There are more names in `pattern` than in `replacement`. The last ", @@ -138,6 +179,11 @@ data_rename <- function(data, ) } + # if we have glue-styled replacement-string, create replacement pattern now + if (glue_style) { + replacement <- .glue_replacement(pattern, replacement) + } + for (i in seq_along(pattern)) { if (!is.na(replacement[i])) { data <- .data_rename(data, pattern[i], replacement[i], safe, verbose) @@ -167,6 +213,84 @@ data_rename <- function(data, } +.glue_replacement <- function(pattern, replacement) { + # this function replaces "glue" tokens into their related + # real names/values. Currently, following tokens are accepted: + # - {col}: replacement is the name of the column (indicated in "pattern") + # - {letter}: replacement is lower-case alphabetically letter, in sequential order + # - {n}: replacement is the number of the variable out of n, that should be renamed + out <- rep_len("", length(pattern)) + + # for alphabetical letters, we prepare a string if we have more than + # 26 columns to rename + if (length(out) > 26) { + long_letters <- paste0( + rep.int(letters[1:26], times = ceiling(length(out) / 26)), + rep(1:ceiling(length(out) / 26), each = 26) + ) + } else { + long_letters <- letters[1:26] + } + long_letters <- long_letters[seq_len(length(out))] + + for (i in seq_along(out)) { + # prepare pattern + column_name <- pattern[i] + out[i] <- replacement + # replace first pre-defined token + out[i] <- gsub( + "(.*)(\\{col\\})(.*)", + replacement = paste0("\\1", column_name, "\\3"), + x = out[i] + ) + # replace second pre-defined token + out[i] <- gsub( + "(.*)(\\{n\\})(.*)", + replacement = paste0("\\1", i, "\\3"), + x = out[i] + ) + # replace third pre-defined token + out[i] <- gsub( + "(.*)(\\{letter\\})(.*)", + replacement = paste0("\\1", long_letters[i], "\\3"), + x = out[i] + ) + # extract all non-standard tokens + matches <- unlist( + regmatches(out[i], gregexpr("\\{([^}]*)\\}", out[i])), + use.names = FALSE + ) + # do we have any additional tokens, i.e. variable names from the environment? + # users can also specify variable names, where the + if (length(matches)) { + # if so, iterate all tokens + for (token in matches) { + # evaluate token-object from the environment + values <- .dynEval( + str2lang(gsub("\\{(.*)\\}", "\\1", token)), + ifnotfound = insight::format_error(paste0( + "The object `", token, "` was not found. Please check if it really exists." + )) + ) + # check for correct length + if (length(values) != length(pattern)) { + insight::format_error(paste0( + "The number of values provided in `", token, "` (", length(values), + " values) do not match the number of columns to rename (", + length(pattern), " columns)." + )) + } + # replace token with values from the object + if (length(values)) { + out[i] <- gsub(token, values[i], out[i], fixed = TRUE) + } + } + } + } + out +} + + # Row.names ---------------------------------------------------------------- #' @rdname data_rename diff --git a/man/categorize.Rd b/man/categorize.Rd index dbecbf5e6..90a5e12b7 100644 --- a/man/categorize.Rd +++ b/man/categorize.Rd @@ -243,12 +243,16 @@ categorize(mtcars$mpg, "equal_length", n_groups = 5, labels = "observed") } \seealso{ \itemize{ -\item Functions to rename stuff: \code{\link[=data_rename]{data_rename()}}, \code{\link[=data_rename_rows]{data_rename_rows()}}, \code{\link[=data_addprefix]{data_addprefix()}}, \code{\link[=data_addsuffix]{data_addsuffix()}} -\item Functions to reorder or remove columns: \code{\link[=data_reorder]{data_reorder()}}, \code{\link[=data_relocate]{data_relocate()}}, \code{\link[=data_remove]{data_remove()}} -\item Functions to reshape, pivot or rotate data frames: \code{\link[=data_to_long]{data_to_long()}}, \code{\link[=data_to_wide]{data_to_wide()}}, \code{\link[=data_rotate]{data_rotate()}} +\item Functions to rename stuff: \code{\link[=data_rename]{data_rename()}}, \code{\link[=data_rename_rows]{data_rename_rows()}}, +\code{\link[=data_addprefix]{data_addprefix()}}, \code{\link[=data_addsuffix]{data_addsuffix()}} +\item Functions to reorder or remove columns: \code{\link[=data_reorder]{data_reorder()}}, \code{\link[=data_relocate]{data_relocate()}}, +\code{\link[=data_remove]{data_remove()}} +\item Functions to reshape, pivot or rotate data frames: \code{\link[=data_to_long]{data_to_long()}}, +\code{\link[=data_to_wide]{data_to_wide()}}, \code{\link[=data_rotate]{data_rotate()}} \item Functions to recode data: \code{\link[=rescale]{rescale()}}, \code{\link[=reverse]{reverse()}}, \code{\link[=categorize]{categorize()}}, \code{\link[=recode_values]{recode_values()}}, \code{\link[=slide]{slide()}} -\item Functions to standardize, normalize, rank-transform: \code{\link[=center]{center()}}, \code{\link[=standardize]{standardize()}}, \code{\link[=normalize]{normalize()}}, \code{\link[=ranktransform]{ranktransform()}}, \code{\link[=winsorize]{winsorize()}} +\item Functions to standardize, normalize, rank-transform: \code{\link[=center]{center()}}, \code{\link[=standardize]{standardize()}}, +\code{\link[=normalize]{normalize()}}, \code{\link[=ranktransform]{ranktransform()}}, \code{\link[=winsorize]{winsorize()}} \item Split and merge data frames: \code{\link[=data_partition]{data_partition()}}, \code{\link[=data_merge]{data_merge()}} \item Functions to find or select columns: \code{\link[=data_select]{data_select()}}, \code{\link[=extract_column_names]{extract_column_names()}} \item Functions to filter rows: \code{\link[=data_match]{data_match()}}, \code{\link[=data_filter]{data_filter()}} diff --git a/man/data_match.Rd b/man/data_match.Rd index a209170ab..0354a44f4 100644 --- a/man/data_match.Rd +++ b/man/data_match.Rd @@ -128,12 +128,16 @@ data_filter(mtcars, fl) } \seealso{ \itemize{ -\item Functions to rename stuff: \code{\link[=data_rename]{data_rename()}}, \code{\link[=data_rename_rows]{data_rename_rows()}}, \code{\link[=data_addprefix]{data_addprefix()}}, \code{\link[=data_addsuffix]{data_addsuffix()}} -\item Functions to reorder or remove columns: \code{\link[=data_reorder]{data_reorder()}}, \code{\link[=data_relocate]{data_relocate()}}, \code{\link[=data_remove]{data_remove()}} -\item Functions to reshape, pivot or rotate data frames: \code{\link[=data_to_long]{data_to_long()}}, \code{\link[=data_to_wide]{data_to_wide()}}, \code{\link[=data_rotate]{data_rotate()}} +\item Functions to rename stuff: \code{\link[=data_rename]{data_rename()}}, \code{\link[=data_rename_rows]{data_rename_rows()}}, +\code{\link[=data_addprefix]{data_addprefix()}}, \code{\link[=data_addsuffix]{data_addsuffix()}} +\item Functions to reorder or remove columns: \code{\link[=data_reorder]{data_reorder()}}, \code{\link[=data_relocate]{data_relocate()}}, +\code{\link[=data_remove]{data_remove()}} +\item Functions to reshape, pivot or rotate data frames: \code{\link[=data_to_long]{data_to_long()}}, +\code{\link[=data_to_wide]{data_to_wide()}}, \code{\link[=data_rotate]{data_rotate()}} \item Functions to recode data: \code{\link[=rescale]{rescale()}}, \code{\link[=reverse]{reverse()}}, \code{\link[=categorize]{categorize()}}, \code{\link[=recode_values]{recode_values()}}, \code{\link[=slide]{slide()}} -\item Functions to standardize, normalize, rank-transform: \code{\link[=center]{center()}}, \code{\link[=standardize]{standardize()}}, \code{\link[=normalize]{normalize()}}, \code{\link[=ranktransform]{ranktransform()}}, \code{\link[=winsorize]{winsorize()}} +\item Functions to standardize, normalize, rank-transform: \code{\link[=center]{center()}}, \code{\link[=standardize]{standardize()}}, +\code{\link[=normalize]{normalize()}}, \code{\link[=ranktransform]{ranktransform()}}, \code{\link[=winsorize]{winsorize()}} \item Split and merge data frames: \code{\link[=data_partition]{data_partition()}}, \code{\link[=data_merge]{data_merge()}} \item Functions to find or select columns: \code{\link[=data_select]{data_select()}}, \code{\link[=extract_column_names]{extract_column_names()}} \item Functions to filter rows: \code{\link[=data_match]{data_match()}}, \code{\link[=data_filter]{data_filter()}} diff --git a/man/data_merge.Rd b/man/data_merge.Rd index a780e76eb..169771b27 100644 --- a/man/data_merge.Rd +++ b/man/data_merge.Rd @@ -190,12 +190,16 @@ data_merge(list(x, y, z), join = "bind", by = "id", id = "source") } \seealso{ \itemize{ -\item Functions to rename stuff: \code{\link[=data_rename]{data_rename()}}, \code{\link[=data_rename_rows]{data_rename_rows()}}, \code{\link[=data_addprefix]{data_addprefix()}}, \code{\link[=data_addsuffix]{data_addsuffix()}} -\item Functions to reorder or remove columns: \code{\link[=data_reorder]{data_reorder()}}, \code{\link[=data_relocate]{data_relocate()}}, \code{\link[=data_remove]{data_remove()}} -\item Functions to reshape, pivot or rotate data frames: \code{\link[=data_to_long]{data_to_long()}}, \code{\link[=data_to_wide]{data_to_wide()}}, \code{\link[=data_rotate]{data_rotate()}} +\item Functions to rename stuff: \code{\link[=data_rename]{data_rename()}}, \code{\link[=data_rename_rows]{data_rename_rows()}}, +\code{\link[=data_addprefix]{data_addprefix()}}, \code{\link[=data_addsuffix]{data_addsuffix()}} +\item Functions to reorder or remove columns: \code{\link[=data_reorder]{data_reorder()}}, \code{\link[=data_relocate]{data_relocate()}}, +\code{\link[=data_remove]{data_remove()}} +\item Functions to reshape, pivot or rotate data frames: \code{\link[=data_to_long]{data_to_long()}}, +\code{\link[=data_to_wide]{data_to_wide()}}, \code{\link[=data_rotate]{data_rotate()}} \item Functions to recode data: \code{\link[=rescale]{rescale()}}, \code{\link[=reverse]{reverse()}}, \code{\link[=categorize]{categorize()}}, \code{\link[=recode_values]{recode_values()}}, \code{\link[=slide]{slide()}} -\item Functions to standardize, normalize, rank-transform: \code{\link[=center]{center()}}, \code{\link[=standardize]{standardize()}}, \code{\link[=normalize]{normalize()}}, \code{\link[=ranktransform]{ranktransform()}}, \code{\link[=winsorize]{winsorize()}} +\item Functions to standardize, normalize, rank-transform: \code{\link[=center]{center()}}, \code{\link[=standardize]{standardize()}}, +\code{\link[=normalize]{normalize()}}, \code{\link[=ranktransform]{ranktransform()}}, \code{\link[=winsorize]{winsorize()}} \item Split and merge data frames: \code{\link[=data_partition]{data_partition()}}, \code{\link[=data_merge]{data_merge()}} \item Functions to find or select columns: \code{\link[=data_select]{data_select()}}, \code{\link[=extract_column_names]{extract_column_names()}} \item Functions to filter rows: \code{\link[=data_match]{data_match()}}, \code{\link[=data_filter]{data_filter()}} diff --git a/man/data_partition.Rd b/man/data_partition.Rd index 1150b4f28..73eb28286 100644 --- a/man/data_partition.Rd +++ b/man/data_partition.Rd @@ -68,12 +68,16 @@ lapply(out, function(i) table(i$Species)) } \seealso{ \itemize{ -\item Functions to rename stuff: \code{\link[=data_rename]{data_rename()}}, \code{\link[=data_rename_rows]{data_rename_rows()}}, \code{\link[=data_addprefix]{data_addprefix()}}, \code{\link[=data_addsuffix]{data_addsuffix()}} -\item Functions to reorder or remove columns: \code{\link[=data_reorder]{data_reorder()}}, \code{\link[=data_relocate]{data_relocate()}}, \code{\link[=data_remove]{data_remove()}} -\item Functions to reshape, pivot or rotate data frames: \code{\link[=data_to_long]{data_to_long()}}, \code{\link[=data_to_wide]{data_to_wide()}}, \code{\link[=data_rotate]{data_rotate()}} +\item Functions to rename stuff: \code{\link[=data_rename]{data_rename()}}, \code{\link[=data_rename_rows]{data_rename_rows()}}, +\code{\link[=data_addprefix]{data_addprefix()}}, \code{\link[=data_addsuffix]{data_addsuffix()}} +\item Functions to reorder or remove columns: \code{\link[=data_reorder]{data_reorder()}}, \code{\link[=data_relocate]{data_relocate()}}, +\code{\link[=data_remove]{data_remove()}} +\item Functions to reshape, pivot or rotate data frames: \code{\link[=data_to_long]{data_to_long()}}, +\code{\link[=data_to_wide]{data_to_wide()}}, \code{\link[=data_rotate]{data_rotate()}} \item Functions to recode data: \code{\link[=rescale]{rescale()}}, \code{\link[=reverse]{reverse()}}, \code{\link[=categorize]{categorize()}}, \code{\link[=recode_values]{recode_values()}}, \code{\link[=slide]{slide()}} -\item Functions to standardize, normalize, rank-transform: \code{\link[=center]{center()}}, \code{\link[=standardize]{standardize()}}, \code{\link[=normalize]{normalize()}}, \code{\link[=ranktransform]{ranktransform()}}, \code{\link[=winsorize]{winsorize()}} +\item Functions to standardize, normalize, rank-transform: \code{\link[=center]{center()}}, \code{\link[=standardize]{standardize()}}, +\code{\link[=normalize]{normalize()}}, \code{\link[=ranktransform]{ranktransform()}}, \code{\link[=winsorize]{winsorize()}} \item Split and merge data frames: \code{\link[=data_partition]{data_partition()}}, \code{\link[=data_merge]{data_merge()}} \item Functions to find or select columns: \code{\link[=data_select]{data_select()}}, \code{\link[=extract_column_names]{extract_column_names()}} \item Functions to filter rows: \code{\link[=data_match]{data_match()}}, \code{\link[=data_filter]{data_filter()}} diff --git a/man/data_relocate.Rd b/man/data_relocate.Rd index 9949b5d27..8f360579d 100644 --- a/man/data_relocate.Rd +++ b/man/data_relocate.Rd @@ -134,12 +134,16 @@ head(data_remove(iris, starts_with("Sepal"))) } \seealso{ \itemize{ -\item Functions to rename stuff: \code{\link[=data_rename]{data_rename()}}, \code{\link[=data_rename_rows]{data_rename_rows()}}, \code{\link[=data_addprefix]{data_addprefix()}}, \code{\link[=data_addsuffix]{data_addsuffix()}} -\item Functions to reorder or remove columns: \code{\link[=data_reorder]{data_reorder()}}, \code{\link[=data_relocate]{data_relocate()}}, \code{\link[=data_remove]{data_remove()}} -\item Functions to reshape, pivot or rotate data frames: \code{\link[=data_to_long]{data_to_long()}}, \code{\link[=data_to_wide]{data_to_wide()}}, \code{\link[=data_rotate]{data_rotate()}} +\item Functions to rename stuff: \code{\link[=data_rename]{data_rename()}}, \code{\link[=data_rename_rows]{data_rename_rows()}}, +\code{\link[=data_addprefix]{data_addprefix()}}, \code{\link[=data_addsuffix]{data_addsuffix()}} +\item Functions to reorder or remove columns: \code{\link[=data_reorder]{data_reorder()}}, \code{\link[=data_relocate]{data_relocate()}}, +\code{\link[=data_remove]{data_remove()}} +\item Functions to reshape, pivot or rotate data frames: \code{\link[=data_to_long]{data_to_long()}}, +\code{\link[=data_to_wide]{data_to_wide()}}, \code{\link[=data_rotate]{data_rotate()}} \item Functions to recode data: \code{\link[=rescale]{rescale()}}, \code{\link[=reverse]{reverse()}}, \code{\link[=categorize]{categorize()}}, \code{\link[=recode_values]{recode_values()}}, \code{\link[=slide]{slide()}} -\item Functions to standardize, normalize, rank-transform: \code{\link[=center]{center()}}, \code{\link[=standardize]{standardize()}}, \code{\link[=normalize]{normalize()}}, \code{\link[=ranktransform]{ranktransform()}}, \code{\link[=winsorize]{winsorize()}} +\item Functions to standardize, normalize, rank-transform: \code{\link[=center]{center()}}, \code{\link[=standardize]{standardize()}}, +\code{\link[=normalize]{normalize()}}, \code{\link[=ranktransform]{ranktransform()}}, \code{\link[=winsorize]{winsorize()}} \item Split and merge data frames: \code{\link[=data_partition]{data_partition()}}, \code{\link[=data_merge]{data_merge()}} \item Functions to find or select columns: \code{\link[=data_select]{data_select()}}, \code{\link[=extract_column_names]{extract_column_names()}} \item Functions to filter rows: \code{\link[=data_match]{data_match()}}, \code{\link[=data_filter]{data_filter()}} diff --git a/man/data_rename.Rd b/man/data_rename.Rd index 2ff779c21..2859c04d7 100644 --- a/man/data_rename.Rd +++ b/man/data_rename.Rd @@ -43,14 +43,17 @@ data_rename_rows(data, rows = NULL) \arguments{ \item{data}{A data frame, or an object that can be coerced to a data frame.} -\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. 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{ = ""} and argument \code{replacement} -will be ignored then).} +\item{pattern}{Character vector. +\itemize{ +\item For \code{data_addprefix()} or \code{data_addsuffix()}, a character string, which +will be added as prefix or suffix to the column names. +\item For \code{data_rename()}, indicates columns that should be selected for +renaming. Can be \code{NULL} (in which case all columns are selected). +\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{ = ""} and argument \code{replacement} will +be ignored then). +}} \item{select}{Variables that will be included when performing the required tasks. Can be either @@ -107,10 +110,39 @@ functions (see 'Details'), this argument may be used as workaround.} \item{...}{Other arguments passed to or from other functions.} -\item{replacement}{Character vector. Indicates the new name of the columns -selected in \code{pattern}. Can be \code{NULL} (in which case column are numbered -in sequential order). If not \code{NULL}, \code{pattern} and \code{replacement} must be -of the same length. If \code{pattern} is a named vector, \code{replacement} is ignored.} +\item{replacement}{Character vector. Can be one of the following: +\itemize{ +\item A character vector that indicates the new names of the columns selected +in \code{pattern}. \code{pattern} and \code{replacement} must be of the same length. +\item \code{NULL}, in which case columns are numbered in sequential order. +\item A string (i.e. character vector of length 1) with a "glue" styled pattern. +Currently supported tokens are: +\itemize{ +\item \code{{col}} which will be replaced by the column name, i.e. the +corresponding value in \code{pattern}. +\item \code{{n}} will be replaced by the number of the variable that is replaced. +\item \code{{letter}} will be replaced by alphabetical letters in sequential order. +If more than 26 letters are required, letters are repeated, but have +sequential numeric indices (e.g., \code{a1} to \code{z1}, followed by \code{a2} to \code{z2}). +\item Finally, the name of a user-defined object that is available in the +environment can be used. Note that the object's name is not allowed to +be one of the pre-defined tokens, \code{"col"}, \code{"n"} and \code{"letter"}. +} + +An example for the use of tokens is... + +\if{html}{\out{
}}\preformatted{data_rename( + mtcars, + pattern = c("am", "vs"), + replacement = "new_name_from_\{col\}" +) +}\if{html}{\out{
}} + +... which would return new column names \code{new_name_from_am} and +\code{new_name_from_vs}. See 'Examples'. +} + +If \code{pattern} is a named vector, \code{replacement} is ignored.} \item{safe}{Do not throw error if for instance the variable to be renamed/removed doesn't exist.} @@ -148,15 +180,28 @@ head(data_rename(iris, NULL)) # Change all head(data_rename(iris, replacement = paste0("Var", 1:5))) + +# Use glue-styled patterns +head(data_rename(mtcars[1:3], c("mpg", "cyl", "disp"), "formerly_{col}")) +head(data_rename(mtcars[1:3], c("mpg", "cyl", "disp"), "{col}_is_column_{n}")) +head(data_rename(mtcars[1:3], c("mpg", "cyl", "disp"), "new_{letter}")) + +# User-defined glue-styled patterns from objects in environment +x <- c("hi", "there", "!") +head(data_rename(mtcars[1:3], c("mpg", "cyl", "disp"), "col_{x}")) } \seealso{ \itemize{ -\item Functions to rename stuff: \code{\link[=data_rename]{data_rename()}}, \code{\link[=data_rename_rows]{data_rename_rows()}}, \code{\link[=data_addprefix]{data_addprefix()}}, \code{\link[=data_addsuffix]{data_addsuffix()}} -\item Functions to reorder or remove columns: \code{\link[=data_reorder]{data_reorder()}}, \code{\link[=data_relocate]{data_relocate()}}, \code{\link[=data_remove]{data_remove()}} -\item Functions to reshape, pivot or rotate data frames: \code{\link[=data_to_long]{data_to_long()}}, \code{\link[=data_to_wide]{data_to_wide()}}, \code{\link[=data_rotate]{data_rotate()}} +\item Functions to rename stuff: \code{\link[=data_rename]{data_rename()}}, \code{\link[=data_rename_rows]{data_rename_rows()}}, +\code{\link[=data_addprefix]{data_addprefix()}}, \code{\link[=data_addsuffix]{data_addsuffix()}} +\item Functions to reorder or remove columns: \code{\link[=data_reorder]{data_reorder()}}, \code{\link[=data_relocate]{data_relocate()}}, +\code{\link[=data_remove]{data_remove()}} +\item Functions to reshape, pivot or rotate data frames: \code{\link[=data_to_long]{data_to_long()}}, +\code{\link[=data_to_wide]{data_to_wide()}}, \code{\link[=data_rotate]{data_rotate()}} \item Functions to recode data: \code{\link[=rescale]{rescale()}}, \code{\link[=reverse]{reverse()}}, \code{\link[=categorize]{categorize()}}, \code{\link[=recode_values]{recode_values()}}, \code{\link[=slide]{slide()}} -\item Functions to standardize, normalize, rank-transform: \code{\link[=center]{center()}}, \code{\link[=standardize]{standardize()}}, \code{\link[=normalize]{normalize()}}, \code{\link[=ranktransform]{ranktransform()}}, \code{\link[=winsorize]{winsorize()}} +\item Functions to standardize, normalize, rank-transform: \code{\link[=center]{center()}}, \code{\link[=standardize]{standardize()}}, +\code{\link[=normalize]{normalize()}}, \code{\link[=ranktransform]{ranktransform()}}, \code{\link[=winsorize]{winsorize()}} \item Split and merge data frames: \code{\link[=data_partition]{data_partition()}}, \code{\link[=data_merge]{data_merge()}} \item Functions to find or select columns: \code{\link[=data_select]{data_select()}}, \code{\link[=extract_column_names]{extract_column_names()}} \item Functions to filter rows: \code{\link[=data_match]{data_match()}}, \code{\link[=data_filter]{data_filter()}} diff --git a/man/data_rotate.Rd b/man/data_rotate.Rd index 604f4f603..25ba9a82b 100644 --- a/man/data_rotate.Rd +++ b/man/data_rotate.Rd @@ -52,12 +52,16 @@ data_rotate(x, colnames = "c") } \seealso{ \itemize{ -\item Functions to rename stuff: \code{\link[=data_rename]{data_rename()}}, \code{\link[=data_rename_rows]{data_rename_rows()}}, \code{\link[=data_addprefix]{data_addprefix()}}, \code{\link[=data_addsuffix]{data_addsuffix()}} -\item Functions to reorder or remove columns: \code{\link[=data_reorder]{data_reorder()}}, \code{\link[=data_relocate]{data_relocate()}}, \code{\link[=data_remove]{data_remove()}} -\item Functions to reshape, pivot or rotate data frames: \code{\link[=data_to_long]{data_to_long()}}, \code{\link[=data_to_wide]{data_to_wide()}}, \code{\link[=data_rotate]{data_rotate()}} +\item Functions to rename stuff: \code{\link[=data_rename]{data_rename()}}, \code{\link[=data_rename_rows]{data_rename_rows()}}, +\code{\link[=data_addprefix]{data_addprefix()}}, \code{\link[=data_addsuffix]{data_addsuffix()}} +\item Functions to reorder or remove columns: \code{\link[=data_reorder]{data_reorder()}}, \code{\link[=data_relocate]{data_relocate()}}, +\code{\link[=data_remove]{data_remove()}} +\item Functions to reshape, pivot or rotate data frames: \code{\link[=data_to_long]{data_to_long()}}, +\code{\link[=data_to_wide]{data_to_wide()}}, \code{\link[=data_rotate]{data_rotate()}} \item Functions to recode data: \code{\link[=rescale]{rescale()}}, \code{\link[=reverse]{reverse()}}, \code{\link[=categorize]{categorize()}}, \code{\link[=recode_values]{recode_values()}}, \code{\link[=slide]{slide()}} -\item Functions to standardize, normalize, rank-transform: \code{\link[=center]{center()}}, \code{\link[=standardize]{standardize()}}, \code{\link[=normalize]{normalize()}}, \code{\link[=ranktransform]{ranktransform()}}, \code{\link[=winsorize]{winsorize()}} +\item Functions to standardize, normalize, rank-transform: \code{\link[=center]{center()}}, \code{\link[=standardize]{standardize()}}, +\code{\link[=normalize]{normalize()}}, \code{\link[=ranktransform]{ranktransform()}}, \code{\link[=winsorize]{winsorize()}} \item Split and merge data frames: \code{\link[=data_partition]{data_partition()}}, \code{\link[=data_merge]{data_merge()}} \item Functions to find or select columns: \code{\link[=data_select]{data_select()}}, \code{\link[=extract_column_names]{extract_column_names()}} \item Functions to filter rows: \code{\link[=data_match]{data_match()}}, \code{\link[=data_filter]{data_filter()}} diff --git a/man/data_to_long.Rd b/man/data_to_long.Rd index 73b54219b..1c77f764c 100644 --- a/man/data_to_long.Rd +++ b/man/data_to_long.Rd @@ -222,12 +222,16 @@ head(even_longer_data) } \seealso{ \itemize{ -\item Functions to rename stuff: \code{\link[=data_rename]{data_rename()}}, \code{\link[=data_rename_rows]{data_rename_rows()}}, \code{\link[=data_addprefix]{data_addprefix()}}, \code{\link[=data_addsuffix]{data_addsuffix()}} -\item Functions to reorder or remove columns: \code{\link[=data_reorder]{data_reorder()}}, \code{\link[=data_relocate]{data_relocate()}}, \code{\link[=data_remove]{data_remove()}} -\item Functions to reshape, pivot or rotate data frames: \code{\link[=data_to_long]{data_to_long()}}, \code{\link[=data_to_wide]{data_to_wide()}}, \code{\link[=data_rotate]{data_rotate()}} +\item Functions to rename stuff: \code{\link[=data_rename]{data_rename()}}, \code{\link[=data_rename_rows]{data_rename_rows()}}, +\code{\link[=data_addprefix]{data_addprefix()}}, \code{\link[=data_addsuffix]{data_addsuffix()}} +\item Functions to reorder or remove columns: \code{\link[=data_reorder]{data_reorder()}}, \code{\link[=data_relocate]{data_relocate()}}, +\code{\link[=data_remove]{data_remove()}} +\item Functions to reshape, pivot or rotate data frames: \code{\link[=data_to_long]{data_to_long()}}, +\code{\link[=data_to_wide]{data_to_wide()}}, \code{\link[=data_rotate]{data_rotate()}} \item Functions to recode data: \code{\link[=rescale]{rescale()}}, \code{\link[=reverse]{reverse()}}, \code{\link[=categorize]{categorize()}}, \code{\link[=recode_values]{recode_values()}}, \code{\link[=slide]{slide()}} -\item Functions to standardize, normalize, rank-transform: \code{\link[=center]{center()}}, \code{\link[=standardize]{standardize()}}, \code{\link[=normalize]{normalize()}}, \code{\link[=ranktransform]{ranktransform()}}, \code{\link[=winsorize]{winsorize()}} +\item Functions to standardize, normalize, rank-transform: \code{\link[=center]{center()}}, \code{\link[=standardize]{standardize()}}, +\code{\link[=normalize]{normalize()}}, \code{\link[=ranktransform]{ranktransform()}}, \code{\link[=winsorize]{winsorize()}} \item Split and merge data frames: \code{\link[=data_partition]{data_partition()}}, \code{\link[=data_merge]{data_merge()}} \item Functions to find or select columns: \code{\link[=data_select]{data_select()}}, \code{\link[=extract_column_names]{extract_column_names()}} \item Functions to filter rows: \code{\link[=data_match]{data_match()}}, \code{\link[=data_filter]{data_filter()}} diff --git a/man/data_to_wide.Rd b/man/data_to_wide.Rd index 8b781fc76..3690eed53 100644 --- a/man/data_to_wide.Rd +++ b/man/data_to_wide.Rd @@ -208,12 +208,16 @@ data_to_wide( } \seealso{ \itemize{ -\item Functions to rename stuff: \code{\link[=data_rename]{data_rename()}}, \code{\link[=data_rename_rows]{data_rename_rows()}}, \code{\link[=data_addprefix]{data_addprefix()}}, \code{\link[=data_addsuffix]{data_addsuffix()}} -\item Functions to reorder or remove columns: \code{\link[=data_reorder]{data_reorder()}}, \code{\link[=data_relocate]{data_relocate()}}, \code{\link[=data_remove]{data_remove()}} -\item Functions to reshape, pivot or rotate data frames: \code{\link[=data_to_long]{data_to_long()}}, \code{\link[=data_to_wide]{data_to_wide()}}, \code{\link[=data_rotate]{data_rotate()}} +\item Functions to rename stuff: \code{\link[=data_rename]{data_rename()}}, \code{\link[=data_rename_rows]{data_rename_rows()}}, +\code{\link[=data_addprefix]{data_addprefix()}}, \code{\link[=data_addsuffix]{data_addsuffix()}} +\item Functions to reorder or remove columns: \code{\link[=data_reorder]{data_reorder()}}, \code{\link[=data_relocate]{data_relocate()}}, +\code{\link[=data_remove]{data_remove()}} +\item Functions to reshape, pivot or rotate data frames: \code{\link[=data_to_long]{data_to_long()}}, +\code{\link[=data_to_wide]{data_to_wide()}}, \code{\link[=data_rotate]{data_rotate()}} \item Functions to recode data: \code{\link[=rescale]{rescale()}}, \code{\link[=reverse]{reverse()}}, \code{\link[=categorize]{categorize()}}, \code{\link[=recode_values]{recode_values()}}, \code{\link[=slide]{slide()}} -\item Functions to standardize, normalize, rank-transform: \code{\link[=center]{center()}}, \code{\link[=standardize]{standardize()}}, \code{\link[=normalize]{normalize()}}, \code{\link[=ranktransform]{ranktransform()}}, \code{\link[=winsorize]{winsorize()}} +\item Functions to standardize, normalize, rank-transform: \code{\link[=center]{center()}}, \code{\link[=standardize]{standardize()}}, +\code{\link[=normalize]{normalize()}}, \code{\link[=ranktransform]{ranktransform()}}, \code{\link[=winsorize]{winsorize()}} \item Split and merge data frames: \code{\link[=data_partition]{data_partition()}}, \code{\link[=data_merge]{data_merge()}} \item Functions to find or select columns: \code{\link[=data_select]{data_select()}}, \code{\link[=extract_column_names]{extract_column_names()}} \item Functions to filter rows: \code{\link[=data_match]{data_match()}}, \code{\link[=data_filter]{data_filter()}} diff --git a/man/extract_column_names.Rd b/man/extract_column_names.Rd index 3ea5da7dc..bb0f9ada8 100644 --- a/man/extract_column_names.Rd +++ b/man/extract_column_names.Rd @@ -175,12 +175,16 @@ head(data_select(mtcars, c(`Miles per Gallon` = "mpg", Cylinders = "cyl"))) } \seealso{ \itemize{ -\item Functions to rename stuff: \code{\link[=data_rename]{data_rename()}}, \code{\link[=data_rename_rows]{data_rename_rows()}}, \code{\link[=data_addprefix]{data_addprefix()}}, \code{\link[=data_addsuffix]{data_addsuffix()}} -\item Functions to reorder or remove columns: \code{\link[=data_reorder]{data_reorder()}}, \code{\link[=data_relocate]{data_relocate()}}, \code{\link[=data_remove]{data_remove()}} -\item Functions to reshape, pivot or rotate data frames: \code{\link[=data_to_long]{data_to_long()}}, \code{\link[=data_to_wide]{data_to_wide()}}, \code{\link[=data_rotate]{data_rotate()}} +\item Functions to rename stuff: \code{\link[=data_rename]{data_rename()}}, \code{\link[=data_rename_rows]{data_rename_rows()}}, +\code{\link[=data_addprefix]{data_addprefix()}}, \code{\link[=data_addsuffix]{data_addsuffix()}} +\item Functions to reorder or remove columns: \code{\link[=data_reorder]{data_reorder()}}, \code{\link[=data_relocate]{data_relocate()}}, +\code{\link[=data_remove]{data_remove()}} +\item Functions to reshape, pivot or rotate data frames: \code{\link[=data_to_long]{data_to_long()}}, +\code{\link[=data_to_wide]{data_to_wide()}}, \code{\link[=data_rotate]{data_rotate()}} \item Functions to recode data: \code{\link[=rescale]{rescale()}}, \code{\link[=reverse]{reverse()}}, \code{\link[=categorize]{categorize()}}, \code{\link[=recode_values]{recode_values()}}, \code{\link[=slide]{slide()}} -\item Functions to standardize, normalize, rank-transform: \code{\link[=center]{center()}}, \code{\link[=standardize]{standardize()}}, \code{\link[=normalize]{normalize()}}, \code{\link[=ranktransform]{ranktransform()}}, \code{\link[=winsorize]{winsorize()}} +\item Functions to standardize, normalize, rank-transform: \code{\link[=center]{center()}}, \code{\link[=standardize]{standardize()}}, +\code{\link[=normalize]{normalize()}}, \code{\link[=ranktransform]{ranktransform()}}, \code{\link[=winsorize]{winsorize()}} \item Split and merge data frames: \code{\link[=data_partition]{data_partition()}}, \code{\link[=data_merge]{data_merge()}} \item Functions to find or select columns: \code{\link[=data_select]{data_select()}}, \code{\link[=extract_column_names]{extract_column_names()}} \item Functions to filter rows: \code{\link[=data_match]{data_match()}}, \code{\link[=data_filter]{data_filter()}} diff --git a/man/recode_values.Rd b/man/recode_values.Rd index dece902f7..66b7c6e32 100644 --- a/man/recode_values.Rd +++ b/man/recode_values.Rd @@ -278,12 +278,16 @@ options(data_recode_pattern = NULL) } \seealso{ \itemize{ -\item Functions to rename stuff: \code{\link[=data_rename]{data_rename()}}, \code{\link[=data_rename_rows]{data_rename_rows()}}, \code{\link[=data_addprefix]{data_addprefix()}}, \code{\link[=data_addsuffix]{data_addsuffix()}} -\item Functions to reorder or remove columns: \code{\link[=data_reorder]{data_reorder()}}, \code{\link[=data_relocate]{data_relocate()}}, \code{\link[=data_remove]{data_remove()}} -\item Functions to reshape, pivot or rotate data frames: \code{\link[=data_to_long]{data_to_long()}}, \code{\link[=data_to_wide]{data_to_wide()}}, \code{\link[=data_rotate]{data_rotate()}} +\item Functions to rename stuff: \code{\link[=data_rename]{data_rename()}}, \code{\link[=data_rename_rows]{data_rename_rows()}}, +\code{\link[=data_addprefix]{data_addprefix()}}, \code{\link[=data_addsuffix]{data_addsuffix()}} +\item Functions to reorder or remove columns: \code{\link[=data_reorder]{data_reorder()}}, \code{\link[=data_relocate]{data_relocate()}}, +\code{\link[=data_remove]{data_remove()}} +\item Functions to reshape, pivot or rotate data frames: \code{\link[=data_to_long]{data_to_long()}}, +\code{\link[=data_to_wide]{data_to_wide()}}, \code{\link[=data_rotate]{data_rotate()}} \item Functions to recode data: \code{\link[=rescale]{rescale()}}, \code{\link[=reverse]{reverse()}}, \code{\link[=categorize]{categorize()}}, \code{\link[=recode_values]{recode_values()}}, \code{\link[=slide]{slide()}} -\item Functions to standardize, normalize, rank-transform: \code{\link[=center]{center()}}, \code{\link[=standardize]{standardize()}}, \code{\link[=normalize]{normalize()}}, \code{\link[=ranktransform]{ranktransform()}}, \code{\link[=winsorize]{winsorize()}} +\item Functions to standardize, normalize, rank-transform: \code{\link[=center]{center()}}, \code{\link[=standardize]{standardize()}}, +\code{\link[=normalize]{normalize()}}, \code{\link[=ranktransform]{ranktransform()}}, \code{\link[=winsorize]{winsorize()}} \item Split and merge data frames: \code{\link[=data_partition]{data_partition()}}, \code{\link[=data_merge]{data_merge()}} \item Functions to find or select columns: \code{\link[=data_select]{data_select()}}, \code{\link[=extract_column_names]{extract_column_names()}} \item Functions to filter rows: \code{\link[=data_match]{data_match()}}, \code{\link[=data_filter]{data_filter()}} diff --git a/man/slide.Rd b/man/slide.Rd index c26943116..2950855f4 100644 --- a/man/slide.Rd +++ b/man/slide.Rd @@ -123,12 +123,16 @@ sapply(mtcars, min) } \seealso{ \itemize{ -\item Functions to rename stuff: \code{\link[=data_rename]{data_rename()}}, \code{\link[=data_rename_rows]{data_rename_rows()}}, \code{\link[=data_addprefix]{data_addprefix()}}, \code{\link[=data_addsuffix]{data_addsuffix()}} -\item Functions to reorder or remove columns: \code{\link[=data_reorder]{data_reorder()}}, \code{\link[=data_relocate]{data_relocate()}}, \code{\link[=data_remove]{data_remove()}} -\item Functions to reshape, pivot or rotate data frames: \code{\link[=data_to_long]{data_to_long()}}, \code{\link[=data_to_wide]{data_to_wide()}}, \code{\link[=data_rotate]{data_rotate()}} +\item Functions to rename stuff: \code{\link[=data_rename]{data_rename()}}, \code{\link[=data_rename_rows]{data_rename_rows()}}, +\code{\link[=data_addprefix]{data_addprefix()}}, \code{\link[=data_addsuffix]{data_addsuffix()}} +\item Functions to reorder or remove columns: \code{\link[=data_reorder]{data_reorder()}}, \code{\link[=data_relocate]{data_relocate()}}, +\code{\link[=data_remove]{data_remove()}} +\item Functions to reshape, pivot or rotate data frames: \code{\link[=data_to_long]{data_to_long()}}, +\code{\link[=data_to_wide]{data_to_wide()}}, \code{\link[=data_rotate]{data_rotate()}} \item Functions to recode data: \code{\link[=rescale]{rescale()}}, \code{\link[=reverse]{reverse()}}, \code{\link[=categorize]{categorize()}}, \code{\link[=recode_values]{recode_values()}}, \code{\link[=slide]{slide()}} -\item Functions to standardize, normalize, rank-transform: \code{\link[=center]{center()}}, \code{\link[=standardize]{standardize()}}, \code{\link[=normalize]{normalize()}}, \code{\link[=ranktransform]{ranktransform()}}, \code{\link[=winsorize]{winsorize()}} +\item Functions to standardize, normalize, rank-transform: \code{\link[=center]{center()}}, \code{\link[=standardize]{standardize()}}, +\code{\link[=normalize]{normalize()}}, \code{\link[=ranktransform]{ranktransform()}}, \code{\link[=winsorize]{winsorize()}} \item Split and merge data frames: \code{\link[=data_partition]{data_partition()}}, \code{\link[=data_merge]{data_merge()}} \item Functions to find or select columns: \code{\link[=data_select]{data_select()}}, \code{\link[=extract_column_names]{extract_column_names()}} \item Functions to filter rows: \code{\link[=data_match]{data_match()}}, \code{\link[=data_filter]{data_filter()}} diff --git a/man/text_format.Rd b/man/text_format.Rd index 14d64b096..16c76e67c 100644 --- a/man/text_format.Rd +++ b/man/text_format.Rd @@ -50,14 +50,17 @@ text elements will not be enclosed.} \item{n}{The number of characters to find.} -\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. 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{ = ""} and argument \code{replacement} -will be ignored then).} +\item{pattern}{Character vector. +\itemize{ +\item For \code{data_addprefix()} or \code{data_addsuffix()}, a character string, which +will be added as prefix or suffix to the column names. +\item For \code{data_rename()}, indicates columns that should be selected for +renaming. Can be \code{NULL} (in which case all columns are selected). +\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{ = ""} and argument \code{replacement} will +be ignored then). +}} } \value{ A character string. diff --git a/man/winsorize.Rd b/man/winsorize.Rd index 3971a1ae7..15fa6af9b 100644 --- a/man/winsorize.Rd +++ b/man/winsorize.Rd @@ -82,12 +82,16 @@ winsorize(iris, threshold = 0.2) } \seealso{ \itemize{ -\item Functions to rename stuff: \code{\link[=data_rename]{data_rename()}}, \code{\link[=data_rename_rows]{data_rename_rows()}}, \code{\link[=data_addprefix]{data_addprefix()}}, \code{\link[=data_addsuffix]{data_addsuffix()}} -\item Functions to reorder or remove columns: \code{\link[=data_reorder]{data_reorder()}}, \code{\link[=data_relocate]{data_relocate()}}, \code{\link[=data_remove]{data_remove()}} -\item Functions to reshape, pivot or rotate data frames: \code{\link[=data_to_long]{data_to_long()}}, \code{\link[=data_to_wide]{data_to_wide()}}, \code{\link[=data_rotate]{data_rotate()}} +\item Functions to rename stuff: \code{\link[=data_rename]{data_rename()}}, \code{\link[=data_rename_rows]{data_rename_rows()}}, +\code{\link[=data_addprefix]{data_addprefix()}}, \code{\link[=data_addsuffix]{data_addsuffix()}} +\item Functions to reorder or remove columns: \code{\link[=data_reorder]{data_reorder()}}, \code{\link[=data_relocate]{data_relocate()}}, +\code{\link[=data_remove]{data_remove()}} +\item Functions to reshape, pivot or rotate data frames: \code{\link[=data_to_long]{data_to_long()}}, +\code{\link[=data_to_wide]{data_to_wide()}}, \code{\link[=data_rotate]{data_rotate()}} \item Functions to recode data: \code{\link[=rescale]{rescale()}}, \code{\link[=reverse]{reverse()}}, \code{\link[=categorize]{categorize()}}, \code{\link[=recode_values]{recode_values()}}, \code{\link[=slide]{slide()}} -\item Functions to standardize, normalize, rank-transform: \code{\link[=center]{center()}}, \code{\link[=standardize]{standardize()}}, \code{\link[=normalize]{normalize()}}, \code{\link[=ranktransform]{ranktransform()}}, \code{\link[=winsorize]{winsorize()}} +\item Functions to standardize, normalize, rank-transform: \code{\link[=center]{center()}}, \code{\link[=standardize]{standardize()}}, +\code{\link[=normalize]{normalize()}}, \code{\link[=ranktransform]{ranktransform()}}, \code{\link[=winsorize]{winsorize()}} \item Split and merge data frames: \code{\link[=data_partition]{data_partition()}}, \code{\link[=data_merge]{data_merge()}} \item Functions to find or select columns: \code{\link[=data_select]{data_select()}}, \code{\link[=extract_column_names]{extract_column_names()}} \item Functions to filter rows: \code{\link[=data_match]{data_match()}}, \code{\link[=data_filter]{data_filter()}} diff --git a/tests/testthat/test-data_rename.R b/tests/testthat/test-data_rename.R index e01c42f8b..79f4427b3 100644 --- a/tests/testthat/test-data_rename.R +++ b/tests/testthat/test-data_rename.R @@ -140,3 +140,89 @@ test_that("data_rename preserves attributes", { expect_named(a1, names(a2)) }) + + +# glue-styled pattern -------------------------- + +test_that("data_rename glue-style", { + data(mtcars) + out <- data_rename(mtcars[1:3], c("mpg", "cyl", "disp"), "formerly_{col}") + expect_named(out, c("formerly_mpg", "formerly_cyl", "formerly_disp")) + out <- data_rename(mtcars[1:3], c("mpg", "cyl", "disp"), "{col}_is_column_{n}") + expect_named(out, c("mpg_is_column_1", "cyl_is_column_2", "disp_is_column_3")) + out <- data_rename(mtcars[1:3], c("mpg", "cyl", "disp"), "new_{letter}") + expect_named(out, c("new_a", "new_b", "new_c")) +}) + +test_that("data_rename enough letters", { + data(efc, package = "datawizard") + data(mtcars) + data(iris) + data(ChickWeight) + data(ToothGrowth) + data(USArrests) + data(airquality) + x <- cbind( + mtcars[1:5, ], iris[1:5, ], efc[1:5, ], ChickWeight[1:5, ], ToothGrowth[1:5, ], + USArrests[1:5, ], airquality[1:5, ] + ) + expect_named( + data_rename(x, replacement = "long_letter_{letter}"), + c( + "long_letter_a1", "long_letter_b1", "long_letter_c1", "long_letter_d1", + "long_letter_e1", "long_letter_f1", "long_letter_g1", "long_letter_h1", + "long_letter_i1", "long_letter_j1", "long_letter_k1", "long_letter_l1", + "long_letter_m1", "long_letter_n1", "long_letter_o1", "long_letter_p1", + "long_letter_q1", "long_letter_r1", "long_letter_s1", "long_letter_t1", + "long_letter_u1", "long_letter_v1", "long_letter_w1", "long_letter_x1", + "long_letter_y1", "long_letter_z1", "long_letter_a2", "long_letter_b2", + "long_letter_c2", "long_letter_d2", "long_letter_e2", "long_letter_f2", + "long_letter_g2", "long_letter_h2", "long_letter_i2", "long_letter_j2", + "long_letter_k2", "long_letter_l2" + ) + ) +}) + +skip_if_not_installed("withr") +withr::with_environment( + new.env(), + test_that("data_rename glue-style, environment", { + data(mtcars) + x <- c("hi", "there", "!") + out <- data_rename(mtcars[1:3], c("mpg", "cyl", "disp"), "col_{x}") + expect_named(out, c("col_hi", "col_there", "col_!")) + expect_error( + data_rename(mtcars[1:3], c("mpg", "disp"), "col_{x}"), + regex = "The number of values" + ) + }) +) + +withr::with_environment( + new.env(), + test_that("data_rename glue-style, object not in environment", { + data(mtcars) + expect_error( + data_rename(mtcars[1:3], c("mpg", "cyl", "disp"), "col_{x}"), + regex = "The object" + ) + }) +) + +withr::with_environment( + new.env(), + test_that("data_rename glue-style, function in environment", { + data(mtcars) + my_fun <- function(cols_to_rename) { + data_rename(head(mtcars)[, 1:6], cols_to_rename, "new_{col}") + } + expect_named( + my_fun(c("mpg", "drat")), + c("new_mpg", "cyl", "disp", "hp", "new_drat", "wt") + ) + expect_named( + my_fun("mpg"), + c("new_mpg", "cyl", "disp", "hp", "drat", "wt") + ) + }) +) From 978cde7652ae57a4863e2f000a6b948a44889b6f Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 27 Nov 2024 17:41:30 +0100 Subject: [PATCH 3/8] Fix inconsitencies in docs (#565) * Fix inconsitencies in docs * Update R/extract_column_names.R Co-authored-by: Etienne Bacher <52219252+etiennebacher@users.noreply.github.com> * update rd (comma) --------- Co-authored-by: Etienne Bacher <52219252+etiennebacher@users.noreply.github.com> --- R/extract_column_names.R | 16 ++++++++++------ man/adjust.Rd | 11 ++++++----- man/assign_labels.Rd | 11 ++++++----- man/categorize.Rd | 11 ++++++----- man/center.Rd | 11 ++++++----- man/convert_na_to.Rd | 11 ++++++----- man/convert_to_na.Rd | 11 ++++++----- man/data_codebook.Rd | 11 ++++++----- man/data_duplicated.Rd | 11 ++++++----- man/data_extract.Rd | 11 ++++++----- man/data_group.Rd | 11 ++++++----- man/data_peek.Rd | 11 ++++++----- man/data_relocate.Rd | 11 ++++++----- man/data_rename.Rd | 11 ++++++----- man/data_replicate.Rd | 11 ++++++----- man/data_separate.Rd | 11 ++++++----- man/data_tabulate.Rd | 11 ++++++----- man/data_to_long.Rd | 11 ++++++----- man/data_unique.Rd | 11 ++++++----- man/data_unite.Rd | 11 ++++++----- man/describe_distribution.Rd | 11 ++++++----- man/extract_column_names.Rd | 16 ++++++++++------ man/labels_to_levels.Rd | 11 ++++++----- man/means_by_group.Rd | 11 ++++++----- man/normalize.Rd | 11 ++++++----- man/ranktransform.Rd | 11 ++++++----- man/recode_values.Rd | 11 ++++++----- man/rescale.Rd | 11 ++++++----- man/reverse.Rd | 11 ++++++----- man/row_count.Rd | 11 ++++++----- man/row_means.Rd | 11 ++++++----- man/slide.Rd | 11 ++++++----- man/standardize.Rd | 11 ++++++----- man/to_factor.Rd | 11 ++++++----- man/to_numeric.Rd | 11 ++++++----- 35 files changed, 218 insertions(+), 177 deletions(-) diff --git a/R/extract_column_names.R b/R/extract_column_names.R index a3d120d3f..b1cb54f35 100644 --- a/R/extract_column_names.R +++ b/R/extract_column_names.R @@ -19,15 +19,16 @@ #' - a vector of negative integers, giving the positions counting from the #' right (e.g., `-1` or `-1:-3`), #' - one of the following select-helpers: `starts_with()`, `ends_with()`, -#' `contains()`, a range using `:` or `regex("")`. `starts_with()`, +#' `contains()`, a range using `:`, or `regex()`. `starts_with()`, #' `ends_with()`, and `contains()` accept several patterns, e.g -#' `starts_with("Sep", "Petal")`. -#' - or a function testing for logical conditions, e.g. `is.numeric()` (or +#' `starts_with("Sep", "Petal")`. `regex()` can be used to define regular +#' expression patterns. +#' - a function testing for logical conditions, e.g. `is.numeric()` (or #' `is.numeric`), or any user-defined function that selects the variables #' for which the function returns `TRUE` (like: `foo <- function(x) mean(x) > 3`), #' - ranges specified via literal variable names, select-helpers (except #' `regex()`) and (user-defined) functions can be negated, i.e. return -#' non-matching elements, when prefixed with a `-`, e.g. `-ends_with("")`, +#' non-matching elements, when prefixed with a `-`, e.g. `-ends_with()`, #' `-is.numeric` or `-(Sepal.Width:Petal.Length)`. **Note:** Negation means #' that matches are _excluded_, and thus, the `exclude` argument can be #' used alternatively. For instance, `select=-ends_with("Length")` (with @@ -48,7 +49,7 @@ #' character string (or a variable containing a character string) and is not #' allowed to be one of the supported select-helpers or a character vector #' of length > 1. `regex = TRUE` is comparable to using one of the two -#' select-helpers, `select = contains("")` or `select = regex("")`, however, +#' select-helpers, `select = contains()` or `select = regex()`, however, #' since the select-helpers may not work when called from inside other #' functions (see 'Details'), this argument may be used as workaround. #' @param verbose Toggle warnings. @@ -131,7 +132,10 @@ #' numeric_mean_35 <- function(x) is.numeric(x) && mean(x, na.rm = TRUE) > 3.5 #' extract_column_names(iris, numeric_mean_35) #' -#' # find range of colum names by range, using character vector +#' # find column names, using range +#' extract_column_names(mtcars, c(cyl:hp, wt)) +#' +#' # find range of column names by range, using character vector #' extract_column_names(mtcars, c("cyl:hp", "wt")) #' #' # rename returned columns for "data_select()" diff --git a/man/adjust.Rd b/man/adjust.Rd index 48b321b8f..c47606ce0 100644 --- a/man/adjust.Rd +++ b/man/adjust.Rd @@ -53,15 +53,16 @@ character vector of variable names including ranges specified via \code{:} \item a vector of negative integers, giving the positions counting from the right (e.g., \code{-1} or \code{-1:-3}), \item one of the following select-helpers: \code{starts_with()}, \code{ends_with()}, -\code{contains()}, a range using \code{:} or \code{regex("")}. \code{starts_with()}, +\code{contains()}, a range using \code{:}, or \code{regex()}. \code{starts_with()}, \code{ends_with()}, and \code{contains()} accept several patterns, e.g -\code{starts_with("Sep", "Petal")}. -\item or a function testing for logical conditions, e.g. \code{is.numeric()} (or +\code{starts_with("Sep", "Petal")}. \code{regex()} can be used to define regular +expression patterns. +\item a function testing for logical conditions, e.g. \code{is.numeric()} (or \code{is.numeric}), or any user-defined function that selects the variables for which the function returns \code{TRUE} (like: \code{foo <- function(x) mean(x) > 3}), \item ranges specified via literal variable names, select-helpers (except \code{regex()}) and (user-defined) functions can be negated, i.e. return -non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with("")}, +non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with()}, \code{-is.numeric} or \code{-(Sepal.Width:Petal.Length)}. \strong{Note:} Negation means that matches are \emph{excluded}, and thus, the \code{exclude} argument can be used alternatively. For instance, \code{select=-ends_with("Length")} (with @@ -102,7 +103,7 @@ treated as regular expression. When \code{regex = TRUE}, select \emph{must} be a character string (or a variable containing a character string) and is not allowed to be one of the supported select-helpers or a character vector of length > 1. \code{regex = TRUE} is comparable to using one of the two -select-helpers, \code{select = contains("")} or \code{select = regex("")}, however, +select-helpers, \code{select = contains()} or \code{select = regex()}, however, since the select-helpers may not work when called from inside other functions (see 'Details'), this argument may be used as workaround.} diff --git a/man/assign_labels.Rd b/man/assign_labels.Rd index e6fd24252..574730e7b 100644 --- a/man/assign_labels.Rd +++ b/man/assign_labels.Rd @@ -48,15 +48,16 @@ character vector of variable names including ranges specified via \code{:} \item a vector of negative integers, giving the positions counting from the right (e.g., \code{-1} or \code{-1:-3}), \item one of the following select-helpers: \code{starts_with()}, \code{ends_with()}, -\code{contains()}, a range using \code{:} or \code{regex("")}. \code{starts_with()}, +\code{contains()}, a range using \code{:}, or \code{regex()}. \code{starts_with()}, \code{ends_with()}, and \code{contains()} accept several patterns, e.g -\code{starts_with("Sep", "Petal")}. -\item or a function testing for logical conditions, e.g. \code{is.numeric()} (or +\code{starts_with("Sep", "Petal")}. \code{regex()} can be used to define regular +expression patterns. +\item a function testing for logical conditions, e.g. \code{is.numeric()} (or \code{is.numeric}), or any user-defined function that selects the variables for which the function returns \code{TRUE} (like: \code{foo <- function(x) mean(x) > 3}), \item ranges specified via literal variable names, select-helpers (except \code{regex()}) and (user-defined) functions can be negated, i.e. return -non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with("")}, +non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with()}, \code{-is.numeric} or \code{-(Sepal.Width:Petal.Length)}. \strong{Note:} Negation means that matches are \emph{excluded}, and thus, the \code{exclude} argument can be used alternatively. For instance, \code{select=-ends_with("Length")} (with @@ -81,7 +82,7 @@ treated as regular expression. When \code{regex = TRUE}, select \emph{must} be a character string (or a variable containing a character string) and is not allowed to be one of the supported select-helpers or a character vector of length > 1. \code{regex = TRUE} is comparable to using one of the two -select-helpers, \code{select = contains("")} or \code{select = regex("")}, however, +select-helpers, \code{select = contains()} or \code{select = regex()}, however, since the select-helpers may not work when called from inside other functions (see 'Details'), this argument may be used as workaround.} diff --git a/man/categorize.Rd b/man/categorize.Rd index 90a5e12b7..0fed929de 100644 --- a/man/categorize.Rd +++ b/man/categorize.Rd @@ -99,15 +99,16 @@ character vector of variable names including ranges specified via \code{:} \item a vector of negative integers, giving the positions counting from the right (e.g., \code{-1} or \code{-1:-3}), \item one of the following select-helpers: \code{starts_with()}, \code{ends_with()}, -\code{contains()}, a range using \code{:} or \code{regex("")}. \code{starts_with()}, +\code{contains()}, a range using \code{:}, or \code{regex()}. \code{starts_with()}, \code{ends_with()}, and \code{contains()} accept several patterns, e.g -\code{starts_with("Sep", "Petal")}. -\item or a function testing for logical conditions, e.g. \code{is.numeric()} (or +\code{starts_with("Sep", "Petal")}. \code{regex()} can be used to define regular +expression patterns. +\item a function testing for logical conditions, e.g. \code{is.numeric()} (or \code{is.numeric}), or any user-defined function that selects the variables for which the function returns \code{TRUE} (like: \code{foo <- function(x) mean(x) > 3}), \item ranges specified via literal variable names, select-helpers (except \code{regex()}) and (user-defined) functions can be negated, i.e. return -non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with("")}, +non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with()}, \code{-is.numeric} or \code{-(Sepal.Width:Petal.Length)}. \strong{Note:} Negation means that matches are \emph{excluded}, and thus, the \code{exclude} argument can be used alternatively. For instance, \code{select=-ends_with("Length")} (with @@ -142,7 +143,7 @@ treated as regular expression. When \code{regex = TRUE}, select \emph{must} be a character string (or a variable containing a character string) and is not allowed to be one of the supported select-helpers or a character vector of length > 1. \code{regex = TRUE} is comparable to using one of the two -select-helpers, \code{select = contains("")} or \code{select = regex("")}, however, +select-helpers, \code{select = contains()} or \code{select = regex()}, however, since the select-helpers may not work when called from inside other functions (see 'Details'), this argument may be used as workaround.} } diff --git a/man/center.Rd b/man/center.Rd index 4774020ab..691f10839 100644 --- a/man/center.Rd +++ b/man/center.Rd @@ -82,15 +82,16 @@ character vector of variable names including ranges specified via \code{:} \item a vector of negative integers, giving the positions counting from the right (e.g., \code{-1} or \code{-1:-3}), \item one of the following select-helpers: \code{starts_with()}, \code{ends_with()}, -\code{contains()}, a range using \code{:} or \code{regex("")}. \code{starts_with()}, +\code{contains()}, a range using \code{:}, or \code{regex()}. \code{starts_with()}, \code{ends_with()}, and \code{contains()} accept several patterns, e.g -\code{starts_with("Sep", "Petal")}. -\item or a function testing for logical conditions, e.g. \code{is.numeric()} (or +\code{starts_with("Sep", "Petal")}. \code{regex()} can be used to define regular +expression patterns. +\item a function testing for logical conditions, e.g. \code{is.numeric()} (or \code{is.numeric}), or any user-defined function that selects the variables for which the function returns \code{TRUE} (like: \code{foo <- function(x) mean(x) > 3}), \item ranges specified via literal variable names, select-helpers (except \code{regex()}) and (user-defined) functions can be negated, i.e. return -non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with("")}, +non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with()}, \code{-is.numeric} or \code{-(Sepal.Width:Petal.Length)}. \strong{Note:} Negation means that matches are \emph{excluded}, and thus, the \code{exclude} argument can be used alternatively. For instance, \code{select=-ends_with("Length")} (with @@ -133,7 +134,7 @@ treated as regular expression. When \code{regex = TRUE}, select \emph{must} be a character string (or a variable containing a character string) and is not allowed to be one of the supported select-helpers or a character vector of length > 1. \code{regex = TRUE} is comparable to using one of the two -select-helpers, \code{select = contains("")} or \code{select = regex("")}, however, +select-helpers, \code{select = contains()} or \code{select = regex()}, however, since the select-helpers may not work when called from inside other functions (see 'Details'), this argument may be used as workaround.} } diff --git a/man/convert_na_to.Rd b/man/convert_na_to.Rd index 702e0eb2e..369e3937d 100644 --- a/man/convert_na_to.Rd +++ b/man/convert_na_to.Rd @@ -51,15 +51,16 @@ character vector of variable names including ranges specified via \code{:} \item a vector of negative integers, giving the positions counting from the right (e.g., \code{-1} or \code{-1:-3}), \item one of the following select-helpers: \code{starts_with()}, \code{ends_with()}, -\code{contains()}, a range using \code{:} or \code{regex("")}. \code{starts_with()}, +\code{contains()}, a range using \code{:}, or \code{regex()}. \code{starts_with()}, \code{ends_with()}, and \code{contains()} accept several patterns, e.g -\code{starts_with("Sep", "Petal")}. -\item or a function testing for logical conditions, e.g. \code{is.numeric()} (or +\code{starts_with("Sep", "Petal")}. \code{regex()} can be used to define regular +expression patterns. +\item a function testing for logical conditions, e.g. \code{is.numeric()} (or \code{is.numeric}), or any user-defined function that selects the variables for which the function returns \code{TRUE} (like: \code{foo <- function(x) mean(x) > 3}), \item ranges specified via literal variable names, select-helpers (except \code{regex()}) and (user-defined) functions can be negated, i.e. return -non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with("")}, +non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with()}, \code{-is.numeric} or \code{-(Sepal.Width:Petal.Length)}. \strong{Note:} Negation means that matches are \emph{excluded}, and thus, the \code{exclude} argument can be used alternatively. For instance, \code{select=-ends_with("Length")} (with @@ -90,7 +91,7 @@ treated as regular expression. When \code{regex = TRUE}, select \emph{must} be a character string (or a variable containing a character string) and is not allowed to be one of the supported select-helpers or a character vector of length > 1. \code{regex = TRUE} is comparable to using one of the two -select-helpers, \code{select = contains("")} or \code{select = regex("")}, however, +select-helpers, \code{select = contains()} or \code{select = regex()}, however, since the select-helpers may not work when called from inside other functions (see 'Details'), this argument may be used as workaround.} } diff --git a/man/convert_to_na.Rd b/man/convert_to_na.Rd index fe308d61e..f56fdd7da 100644 --- a/man/convert_to_na.Rd +++ b/man/convert_to_na.Rd @@ -54,15 +54,16 @@ character vector of variable names including ranges specified via \code{:} \item a vector of negative integers, giving the positions counting from the right (e.g., \code{-1} or \code{-1:-3}), \item one of the following select-helpers: \code{starts_with()}, \code{ends_with()}, -\code{contains()}, a range using \code{:} or \code{regex("")}. \code{starts_with()}, +\code{contains()}, a range using \code{:}, or \code{regex()}. \code{starts_with()}, \code{ends_with()}, and \code{contains()} accept several patterns, e.g -\code{starts_with("Sep", "Petal")}. -\item or a function testing for logical conditions, e.g. \code{is.numeric()} (or +\code{starts_with("Sep", "Petal")}. \code{regex()} can be used to define regular +expression patterns. +\item a function testing for logical conditions, e.g. \code{is.numeric()} (or \code{is.numeric}), or any user-defined function that selects the variables for which the function returns \code{TRUE} (like: \code{foo <- function(x) mean(x) > 3}), \item ranges specified via literal variable names, select-helpers (except \code{regex()}) and (user-defined) functions can be negated, i.e. return -non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with("")}, +non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with()}, \code{-is.numeric} or \code{-(Sepal.Width:Petal.Length)}. \strong{Note:} Negation means that matches are \emph{excluded}, and thus, the \code{exclude} argument can be used alternatively. For instance, \code{select=-ends_with("Length")} (with @@ -87,7 +88,7 @@ treated as regular expression. When \code{regex = TRUE}, select \emph{must} be a character string (or a variable containing a character string) and is not allowed to be one of the supported select-helpers or a character vector of length > 1. \code{regex = TRUE} is comparable to using one of the two -select-helpers, \code{select = contains("")} or \code{select = regex("")}, however, +select-helpers, \code{select = contains()} or \code{select = regex()}, however, since the select-helpers may not work when called from inside other functions (see 'Details'), this argument may be used as workaround.} } diff --git a/man/data_codebook.Rd b/man/data_codebook.Rd index d5a542be4..4fcddc1ff 100644 --- a/man/data_codebook.Rd +++ b/man/data_codebook.Rd @@ -44,15 +44,16 @@ character vector of variable names including ranges specified via \code{:} \item a vector of negative integers, giving the positions counting from the right (e.g., \code{-1} or \code{-1:-3}), \item one of the following select-helpers: \code{starts_with()}, \code{ends_with()}, -\code{contains()}, a range using \code{:} or \code{regex("")}. \code{starts_with()}, +\code{contains()}, a range using \code{:}, or \code{regex()}. \code{starts_with()}, \code{ends_with()}, and \code{contains()} accept several patterns, e.g -\code{starts_with("Sep", "Petal")}. -\item or a function testing for logical conditions, e.g. \code{is.numeric()} (or +\code{starts_with("Sep", "Petal")}. \code{regex()} can be used to define regular +expression patterns. +\item a function testing for logical conditions, e.g. \code{is.numeric()} (or \code{is.numeric}), or any user-defined function that selects the variables for which the function returns \code{TRUE} (like: \code{foo <- function(x) mean(x) > 3}), \item ranges specified via literal variable names, select-helpers (except \code{regex()}) and (user-defined) functions can be negated, i.e. return -non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with("")}, +non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with()}, \code{-is.numeric} or \code{-(Sepal.Width:Petal.Length)}. \strong{Note:} Negation means that matches are \emph{excluded}, and thus, the \code{exclude} argument can be used alternatively. For instance, \code{select=-ends_with("Length")} (with @@ -94,7 +95,7 @@ treated as regular expression. When \code{regex = TRUE}, select \emph{must} be a character string (or a variable containing a character string) and is not allowed to be one of the supported select-helpers or a character vector of length > 1. \code{regex = TRUE} is comparable to using one of the two -select-helpers, \code{select = contains("")} or \code{select = regex("")}, however, +select-helpers, \code{select = contains()} or \code{select = regex()}, however, since the select-helpers may not work when called from inside other functions (see 'Details'), this argument may be used as workaround.} diff --git a/man/data_duplicated.Rd b/man/data_duplicated.Rd index 88624c8c8..30d8a455d 100644 --- a/man/data_duplicated.Rd +++ b/man/data_duplicated.Rd @@ -30,15 +30,16 @@ character vector of variable names including ranges specified via \code{:} \item a vector of negative integers, giving the positions counting from the right (e.g., \code{-1} or \code{-1:-3}), \item one of the following select-helpers: \code{starts_with()}, \code{ends_with()}, -\code{contains()}, a range using \code{:} or \code{regex("")}. \code{starts_with()}, +\code{contains()}, a range using \code{:}, or \code{regex()}. \code{starts_with()}, \code{ends_with()}, and \code{contains()} accept several patterns, e.g -\code{starts_with("Sep", "Petal")}. -\item or a function testing for logical conditions, e.g. \code{is.numeric()} (or +\code{starts_with("Sep", "Petal")}. \code{regex()} can be used to define regular +expression patterns. +\item a function testing for logical conditions, e.g. \code{is.numeric()} (or \code{is.numeric}), or any user-defined function that selects the variables for which the function returns \code{TRUE} (like: \code{foo <- function(x) mean(x) > 3}), \item ranges specified via literal variable names, select-helpers (except \code{regex()}) and (user-defined) functions can be negated, i.e. return -non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with("")}, +non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with()}, \code{-is.numeric} or \code{-(Sepal.Width:Petal.Length)}. \strong{Note:} Negation means that matches are \emph{excluded}, and thus, the \code{exclude} argument can be used alternatively. For instance, \code{select=-ends_with("Length")} (with @@ -63,7 +64,7 @@ treated as regular expression. When \code{regex = TRUE}, select \emph{must} be a character string (or a variable containing a character string) and is not allowed to be one of the supported select-helpers or a character vector of length > 1. \code{regex = TRUE} is comparable to using one of the two -select-helpers, \code{select = contains("")} or \code{select = regex("")}, however, +select-helpers, \code{select = contains()} or \code{select = regex()}, however, since the select-helpers may not work when called from inside other functions (see 'Details'), this argument may be used as workaround.} diff --git a/man/data_extract.Rd b/man/data_extract.Rd index 0b544e710..91b5ee29a 100644 --- a/man/data_extract.Rd +++ b/man/data_extract.Rd @@ -37,15 +37,16 @@ character vector of variable names including ranges specified via \code{:} \item a vector of negative integers, giving the positions counting from the right (e.g., \code{-1} or \code{-1:-3}), \item one of the following select-helpers: \code{starts_with()}, \code{ends_with()}, -\code{contains()}, a range using \code{:} or \code{regex("")}. \code{starts_with()}, +\code{contains()}, a range using \code{:}, or \code{regex()}. \code{starts_with()}, \code{ends_with()}, and \code{contains()} accept several patterns, e.g -\code{starts_with("Sep", "Petal")}. -\item or a function testing for logical conditions, e.g. \code{is.numeric()} (or +\code{starts_with("Sep", "Petal")}. \code{regex()} can be used to define regular +expression patterns. +\item a function testing for logical conditions, e.g. \code{is.numeric()} (or \code{is.numeric}), or any user-defined function that selects the variables for which the function returns \code{TRUE} (like: \code{foo <- function(x) mean(x) > 3}), \item ranges specified via literal variable names, select-helpers (except \code{regex()}) and (user-defined) functions can be negated, i.e. return -non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with("")}, +non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with()}, \code{-is.numeric} or \code{-(Sepal.Width:Petal.Length)}. \strong{Note:} Negation means that matches are \emph{excluded}, and thus, the \code{exclude} argument can be used alternatively. For instance, \code{select=-ends_with("Length")} (with @@ -88,7 +89,7 @@ treated as regular expression. When \code{regex = TRUE}, select \emph{must} be a character string (or a variable containing a character string) and is not allowed to be one of the supported select-helpers or a character vector of length > 1. \code{regex = TRUE} is comparable to using one of the two -select-helpers, \code{select = contains("")} or \code{select = regex("")}, however, +select-helpers, \code{select = contains()} or \code{select = regex()}, however, since the select-helpers may not work when called from inside other functions (see 'Details'), this argument may be used as workaround.} diff --git a/man/data_group.Rd b/man/data_group.Rd index 9cb55de5d..c360f7b5e 100644 --- a/man/data_group.Rd +++ b/man/data_group.Rd @@ -34,15 +34,16 @@ character vector of variable names including ranges specified via \code{:} \item a vector of negative integers, giving the positions counting from the right (e.g., \code{-1} or \code{-1:-3}), \item one of the following select-helpers: \code{starts_with()}, \code{ends_with()}, -\code{contains()}, a range using \code{:} or \code{regex("")}. \code{starts_with()}, +\code{contains()}, a range using \code{:}, or \code{regex()}. \code{starts_with()}, \code{ends_with()}, and \code{contains()} accept several patterns, e.g -\code{starts_with("Sep", "Petal")}. -\item or a function testing for logical conditions, e.g. \code{is.numeric()} (or +\code{starts_with("Sep", "Petal")}. \code{regex()} can be used to define regular +expression patterns. +\item a function testing for logical conditions, e.g. \code{is.numeric()} (or \code{is.numeric}), or any user-defined function that selects the variables for which the function returns \code{TRUE} (like: \code{foo <- function(x) mean(x) > 3}), \item ranges specified via literal variable names, select-helpers (except \code{regex()}) and (user-defined) functions can be negated, i.e. return -non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with("")}, +non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with()}, \code{-is.numeric} or \code{-(Sepal.Width:Petal.Length)}. \strong{Note:} Negation means that matches are \emph{excluded}, and thus, the \code{exclude} argument can be used alternatively. For instance, \code{select=-ends_with("Length")} (with @@ -67,7 +68,7 @@ treated as regular expression. When \code{regex = TRUE}, select \emph{must} be a character string (or a variable containing a character string) and is not allowed to be one of the supported select-helpers or a character vector of length > 1. \code{regex = TRUE} is comparable to using one of the two -select-helpers, \code{select = contains("")} or \code{select = regex("")}, however, +select-helpers, \code{select = contains()} or \code{select = regex()}, however, since the select-helpers may not work when called from inside other functions (see 'Details'), this argument may be used as workaround.} diff --git a/man/data_peek.Rd b/man/data_peek.Rd index 9524c70ec..5d5e2e18e 100644 --- a/man/data_peek.Rd +++ b/man/data_peek.Rd @@ -37,15 +37,16 @@ character vector of variable names including ranges specified via \code{:} \item a vector of negative integers, giving the positions counting from the right (e.g., \code{-1} or \code{-1:-3}), \item one of the following select-helpers: \code{starts_with()}, \code{ends_with()}, -\code{contains()}, a range using \code{:} or \code{regex("")}. \code{starts_with()}, +\code{contains()}, a range using \code{:}, or \code{regex()}. \code{starts_with()}, \code{ends_with()}, and \code{contains()} accept several patterns, e.g -\code{starts_with("Sep", "Petal")}. -\item or a function testing for logical conditions, e.g. \code{is.numeric()} (or +\code{starts_with("Sep", "Petal")}. \code{regex()} can be used to define regular +expression patterns. +\item a function testing for logical conditions, e.g. \code{is.numeric()} (or \code{is.numeric}), or any user-defined function that selects the variables for which the function returns \code{TRUE} (like: \code{foo <- function(x) mean(x) > 3}), \item ranges specified via literal variable names, select-helpers (except \code{regex()}) and (user-defined) functions can be negated, i.e. return -non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with("")}, +non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with()}, \code{-is.numeric} or \code{-(Sepal.Width:Petal.Length)}. \strong{Note:} Negation means that matches are \emph{excluded}, and thus, the \code{exclude} argument can be used alternatively. For instance, \code{select=-ends_with("Length")} (with @@ -70,7 +71,7 @@ treated as regular expression. When \code{regex = TRUE}, select \emph{must} be a character string (or a variable containing a character string) and is not allowed to be one of the supported select-helpers or a character vector of length > 1. \code{regex = TRUE} is comparable to using one of the two -select-helpers, \code{select = contains("")} or \code{select = regex("")}, however, +select-helpers, \code{select = contains()} or \code{select = regex()}, however, since the select-helpers may not work when called from inside other functions (see 'Details'), this argument may be used as workaround.} diff --git a/man/data_relocate.Rd b/man/data_relocate.Rd index 8f360579d..5991691cb 100644 --- a/man/data_relocate.Rd +++ b/man/data_relocate.Rd @@ -54,15 +54,16 @@ character vector of variable names including ranges specified via \code{:} \item a vector of negative integers, giving the positions counting from the right (e.g., \code{-1} or \code{-1:-3}), \item one of the following select-helpers: \code{starts_with()}, \code{ends_with()}, -\code{contains()}, a range using \code{:} or \code{regex("")}. \code{starts_with()}, +\code{contains()}, a range using \code{:}, or \code{regex()}. \code{starts_with()}, \code{ends_with()}, and \code{contains()} accept several patterns, e.g -\code{starts_with("Sep", "Petal")}. -\item or a function testing for logical conditions, e.g. \code{is.numeric()} (or +\code{starts_with("Sep", "Petal")}. \code{regex()} can be used to define regular +expression patterns. +\item a function testing for logical conditions, e.g. \code{is.numeric()} (or \code{is.numeric}), or any user-defined function that selects the variables for which the function returns \code{TRUE} (like: \code{foo <- function(x) mean(x) > 3}), \item ranges specified via literal variable names, select-helpers (except \code{regex()}) and (user-defined) functions can be negated, i.e. return -non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with("")}, +non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with()}, \code{-is.numeric} or \code{-(Sepal.Width:Petal.Length)}. \strong{Note:} Negation means that matches are \emph{excluded}, and thus, the \code{exclude} argument can be used alternatively. For instance, \code{select=-ends_with("Length")} (with @@ -89,7 +90,7 @@ treated as regular expression. When \code{regex = TRUE}, select \emph{must} be a character string (or a variable containing a character string) and is not allowed to be one of the supported select-helpers or a character vector of length > 1. \code{regex = TRUE} is comparable to using one of the two -select-helpers, \code{select = contains("")} or \code{select = regex("")}, however, +select-helpers, \code{select = contains()} or \code{select = regex()}, however, since the select-helpers may not work when called from inside other functions (see 'Details'), this argument may be used as workaround.} diff --git a/man/data_rename.Rd b/man/data_rename.Rd index 2859c04d7..3fea0fac4 100644 --- a/man/data_rename.Rd +++ b/man/data_rename.Rd @@ -69,15 +69,16 @@ character vector of variable names including ranges specified via \code{:} \item a vector of negative integers, giving the positions counting from the right (e.g., \code{-1} or \code{-1:-3}), \item one of the following select-helpers: \code{starts_with()}, \code{ends_with()}, -\code{contains()}, a range using \code{:} or \code{regex("")}. \code{starts_with()}, +\code{contains()}, a range using \code{:}, or \code{regex()}. \code{starts_with()}, \code{ends_with()}, and \code{contains()} accept several patterns, e.g -\code{starts_with("Sep", "Petal")}. -\item or a function testing for logical conditions, e.g. \code{is.numeric()} (or +\code{starts_with("Sep", "Petal")}. \code{regex()} can be used to define regular +expression patterns. +\item a function testing for logical conditions, e.g. \code{is.numeric()} (or \code{is.numeric}), or any user-defined function that selects the variables for which the function returns \code{TRUE} (like: \code{foo <- function(x) mean(x) > 3}), \item ranges specified via literal variable names, select-helpers (except \code{regex()}) and (user-defined) functions can be negated, i.e. return -non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with("")}, +non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with()}, \code{-is.numeric} or \code{-(Sepal.Width:Petal.Length)}. \strong{Note:} Negation means that matches are \emph{excluded}, and thus, the \code{exclude} argument can be used alternatively. For instance, \code{select=-ends_with("Length")} (with @@ -102,7 +103,7 @@ treated as regular expression. When \code{regex = TRUE}, select \emph{must} be a character string (or a variable containing a character string) and is not allowed to be one of the supported select-helpers or a character vector of length > 1. \code{regex = TRUE} is comparable to using one of the two -select-helpers, \code{select = contains("")} or \code{select = regex("")}, however, +select-helpers, \code{select = contains()} or \code{select = regex()}, however, since the select-helpers may not work when called from inside other functions (see 'Details'), this argument may be used as workaround.} diff --git a/man/data_replicate.Rd b/man/data_replicate.Rd index 5a427d570..0be353c66 100644 --- a/man/data_replicate.Rd +++ b/man/data_replicate.Rd @@ -37,15 +37,16 @@ character vector of variable names including ranges specified via \code{:} \item a vector of negative integers, giving the positions counting from the right (e.g., \code{-1} or \code{-1:-3}), \item one of the following select-helpers: \code{starts_with()}, \code{ends_with()}, -\code{contains()}, a range using \code{:} or \code{regex("")}. \code{starts_with()}, +\code{contains()}, a range using \code{:}, or \code{regex()}. \code{starts_with()}, \code{ends_with()}, and \code{contains()} accept several patterns, e.g -\code{starts_with("Sep", "Petal")}. -\item or a function testing for logical conditions, e.g. \code{is.numeric()} (or +\code{starts_with("Sep", "Petal")}. \code{regex()} can be used to define regular +expression patterns. +\item a function testing for logical conditions, e.g. \code{is.numeric()} (or \code{is.numeric}), or any user-defined function that selects the variables for which the function returns \code{TRUE} (like: \code{foo <- function(x) mean(x) > 3}), \item ranges specified via literal variable names, select-helpers (except \code{regex()}) and (user-defined) functions can be negated, i.e. return -non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with("")}, +non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with()}, \code{-is.numeric} or \code{-(Sepal.Width:Petal.Length)}. \strong{Note:} Negation means that matches are \emph{excluded}, and thus, the \code{exclude} argument can be used alternatively. For instance, \code{select=-ends_with("Length")} (with @@ -76,7 +77,7 @@ treated as regular expression. When \code{regex = TRUE}, select \emph{must} be a character string (or a variable containing a character string) and is not allowed to be one of the supported select-helpers or a character vector of length > 1. \code{regex = TRUE} is comparable to using one of the two -select-helpers, \code{select = contains("")} or \code{select = regex("")}, however, +select-helpers, \code{select = contains()} or \code{select = regex()}, however, since the select-helpers may not work when called from inside other functions (see 'Details'), this argument may be used as workaround.} diff --git a/man/data_separate.Rd b/man/data_separate.Rd index 7c951f81c..b85b01916 100644 --- a/man/data_separate.Rd +++ b/man/data_separate.Rd @@ -40,15 +40,16 @@ character vector of variable names including ranges specified via \code{:} \item a vector of negative integers, giving the positions counting from the right (e.g., \code{-1} or \code{-1:-3}), \item one of the following select-helpers: \code{starts_with()}, \code{ends_with()}, -\code{contains()}, a range using \code{:} or \code{regex("")}. \code{starts_with()}, +\code{contains()}, a range using \code{:}, or \code{regex()}. \code{starts_with()}, \code{ends_with()}, and \code{contains()} accept several patterns, e.g -\code{starts_with("Sep", "Petal")}. -\item or a function testing for logical conditions, e.g. \code{is.numeric()} (or +\code{starts_with("Sep", "Petal")}. \code{regex()} can be used to define regular +expression patterns. +\item a function testing for logical conditions, e.g. \code{is.numeric()} (or \code{is.numeric}), or any user-defined function that selects the variables for which the function returns \code{TRUE} (like: \code{foo <- function(x) mean(x) > 3}), \item ranges specified via literal variable names, select-helpers (except \code{regex()}) and (user-defined) functions can be negated, i.e. return -non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with("")}, +non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with()}, \code{-is.numeric} or \code{-(Sepal.Width:Petal.Length)}. \strong{Note:} Negation means that matches are \emph{excluded}, and thus, the \code{exclude} argument can be used alternatively. For instance, \code{select=-ends_with("Length")} (with @@ -120,7 +121,7 @@ treated as regular expression. When \code{regex = TRUE}, select \emph{must} be a character string (or a variable containing a character string) and is not allowed to be one of the supported select-helpers or a character vector of length > 1. \code{regex = TRUE} is comparable to using one of the two -select-helpers, \code{select = contains("")} or \code{select = regex("")}, however, +select-helpers, \code{select = contains()} or \code{select = regex()}, however, since the select-helpers may not work when called from inside other functions (see 'Details'), this argument may be used as workaround.} diff --git a/man/data_tabulate.Rd b/man/data_tabulate.Rd index b28a26ede..ac91ead65 100644 --- a/man/data_tabulate.Rd +++ b/man/data_tabulate.Rd @@ -89,15 +89,16 @@ character vector of variable names including ranges specified via \code{:} \item a vector of negative integers, giving the positions counting from the right (e.g., \code{-1} or \code{-1:-3}), \item one of the following select-helpers: \code{starts_with()}, \code{ends_with()}, -\code{contains()}, a range using \code{:} or \code{regex("")}. \code{starts_with()}, +\code{contains()}, a range using \code{:}, or \code{regex()}. \code{starts_with()}, \code{ends_with()}, and \code{contains()} accept several patterns, e.g -\code{starts_with("Sep", "Petal")}. -\item or a function testing for logical conditions, e.g. \code{is.numeric()} (or +\code{starts_with("Sep", "Petal")}. \code{regex()} can be used to define regular +expression patterns. +\item a function testing for logical conditions, e.g. \code{is.numeric()} (or \code{is.numeric}), or any user-defined function that selects the variables for which the function returns \code{TRUE} (like: \code{foo <- function(x) mean(x) > 3}), \item ranges specified via literal variable names, select-helpers (except \code{regex()}) and (user-defined) functions can be negated, i.e. return -non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with("")}, +non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with()}, \code{-is.numeric} or \code{-(Sepal.Width:Petal.Length)}. \strong{Note:} Negation means that matches are \emph{excluded}, and thus, the \code{exclude} argument can be used alternatively. For instance, \code{select=-ends_with("Length")} (with @@ -122,7 +123,7 @@ treated as regular expression. When \code{regex = TRUE}, select \emph{must} be a character string (or a variable containing a character string) and is not allowed to be one of the supported select-helpers or a character vector of length > 1. \code{regex = TRUE} is comparable to using one of the two -select-helpers, \code{select = contains("")} or \code{select = regex("")}, however, +select-helpers, \code{select = contains()} or \code{select = regex()}, however, since the select-helpers may not work when called from inside other functions (see 'Details'), this argument may be used as workaround.} diff --git a/man/data_to_long.Rd b/man/data_to_long.Rd index 1c77f764c..3e587c049 100644 --- a/man/data_to_long.Rd +++ b/man/data_to_long.Rd @@ -55,15 +55,16 @@ character vector of variable names including ranges specified via \code{:} \item a vector of negative integers, giving the positions counting from the right (e.g., \code{-1} or \code{-1:-3}), \item one of the following select-helpers: \code{starts_with()}, \code{ends_with()}, -\code{contains()}, a range using \code{:} or \code{regex("")}. \code{starts_with()}, +\code{contains()}, a range using \code{:}, or \code{regex()}. \code{starts_with()}, \code{ends_with()}, and \code{contains()} accept several patterns, e.g -\code{starts_with("Sep", "Petal")}. -\item or a function testing for logical conditions, e.g. \code{is.numeric()} (or +\code{starts_with("Sep", "Petal")}. \code{regex()} can be used to define regular +expression patterns. +\item a function testing for logical conditions, e.g. \code{is.numeric()} (or \code{is.numeric}), or any user-defined function that selects the variables for which the function returns \code{TRUE} (like: \code{foo <- function(x) mean(x) > 3}), \item ranges specified via literal variable names, select-helpers (except \code{regex()}) and (user-defined) functions can be negated, i.e. return -non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with("")}, +non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with()}, \code{-is.numeric} or \code{-(Sepal.Width:Petal.Length)}. \strong{Note:} Negation means that matches are \emph{excluded}, and thus, the \code{exclude} argument can be used alternatively. For instance, \code{select=-ends_with("Length")} (with @@ -109,7 +110,7 @@ treated as regular expression. When \code{regex = TRUE}, select \emph{must} be a character string (or a variable containing a character string) and is not allowed to be one of the supported select-helpers or a character vector of length > 1. \code{regex = TRUE} is comparable to using one of the two -select-helpers, \code{select = contains("")} or \code{select = regex("")}, however, +select-helpers, \code{select = contains()} or \code{select = regex()}, however, since the select-helpers may not work when called from inside other functions (see 'Details'), this argument may be used as workaround.} diff --git a/man/data_unique.Rd b/man/data_unique.Rd index a0a70b92a..e0911901d 100644 --- a/man/data_unique.Rd +++ b/man/data_unique.Rd @@ -31,15 +31,16 @@ character vector of variable names including ranges specified via \code{:} \item a vector of negative integers, giving the positions counting from the right (e.g., \code{-1} or \code{-1:-3}), \item one of the following select-helpers: \code{starts_with()}, \code{ends_with()}, -\code{contains()}, a range using \code{:} or \code{regex("")}. \code{starts_with()}, +\code{contains()}, a range using \code{:}, or \code{regex()}. \code{starts_with()}, \code{ends_with()}, and \code{contains()} accept several patterns, e.g -\code{starts_with("Sep", "Petal")}. -\item or a function testing for logical conditions, e.g. \code{is.numeric()} (or +\code{starts_with("Sep", "Petal")}. \code{regex()} can be used to define regular +expression patterns. +\item a function testing for logical conditions, e.g. \code{is.numeric()} (or \code{is.numeric}), or any user-defined function that selects the variables for which the function returns \code{TRUE} (like: \code{foo <- function(x) mean(x) > 3}), \item ranges specified via literal variable names, select-helpers (except \code{regex()}) and (user-defined) functions can be negated, i.e. return -non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with("")}, +non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with()}, \code{-is.numeric} or \code{-(Sepal.Width:Petal.Length)}. \strong{Note:} Negation means that matches are \emph{excluded}, and thus, the \code{exclude} argument can be used alternatively. For instance, \code{select=-ends_with("Length")} (with @@ -67,7 +68,7 @@ treated as regular expression. When \code{regex = TRUE}, select \emph{must} be a character string (or a variable containing a character string) and is not allowed to be one of the supported select-helpers or a character vector of length > 1. \code{regex = TRUE} is comparable to using one of the two -select-helpers, \code{select = contains("")} or \code{select = regex("")}, however, +select-helpers, \code{select = contains()} or \code{select = regex()}, however, since the select-helpers may not work when called from inside other functions (see 'Details'), this argument may be used as workaround.} diff --git a/man/data_unite.Rd b/man/data_unite.Rd index 369fd33d8..c9893933e 100644 --- a/man/data_unite.Rd +++ b/man/data_unite.Rd @@ -37,15 +37,16 @@ character vector of variable names including ranges specified via \code{:} \item a vector of negative integers, giving the positions counting from the right (e.g., \code{-1} or \code{-1:-3}), \item one of the following select-helpers: \code{starts_with()}, \code{ends_with()}, -\code{contains()}, a range using \code{:} or \code{regex("")}. \code{starts_with()}, +\code{contains()}, a range using \code{:}, or \code{regex()}. \code{starts_with()}, \code{ends_with()}, and \code{contains()} accept several patterns, e.g -\code{starts_with("Sep", "Petal")}. -\item or a function testing for logical conditions, e.g. \code{is.numeric()} (or +\code{starts_with("Sep", "Petal")}. \code{regex()} can be used to define regular +expression patterns. +\item a function testing for logical conditions, e.g. \code{is.numeric()} (or \code{is.numeric}), or any user-defined function that selects the variables for which the function returns \code{TRUE} (like: \code{foo <- function(x) mean(x) > 3}), \item ranges specified via literal variable names, select-helpers (except \code{regex()}) and (user-defined) functions can be negated, i.e. return -non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with("")}, +non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with()}, \code{-is.numeric} or \code{-(Sepal.Width:Petal.Length)}. \strong{Note:} Negation means that matches are \emph{excluded}, and thus, the \code{exclude} argument can be used alternatively. For instance, \code{select=-ends_with("Length")} (with @@ -82,7 +83,7 @@ treated as regular expression. When \code{regex = TRUE}, select \emph{must} be a character string (or a variable containing a character string) and is not allowed to be one of the supported select-helpers or a character vector of length > 1. \code{regex = TRUE} is comparable to using one of the two -select-helpers, \code{select = contains("")} or \code{select = regex("")}, however, +select-helpers, \code{select = contains()} or \code{select = regex()}, however, since the select-helpers may not work when called from inside other functions (see 'Details'), this argument may be used as workaround.} diff --git a/man/describe_distribution.Rd b/man/describe_distribution.Rd index 80b69e115..4837834ac 100644 --- a/man/describe_distribution.Rd +++ b/man/describe_distribution.Rd @@ -96,15 +96,16 @@ character vector of variable names including ranges specified via \code{:} \item a vector of negative integers, giving the positions counting from the right (e.g., \code{-1} or \code{-1:-3}), \item one of the following select-helpers: \code{starts_with()}, \code{ends_with()}, -\code{contains()}, a range using \code{:} or \code{regex("")}. \code{starts_with()}, +\code{contains()}, a range using \code{:}, or \code{regex()}. \code{starts_with()}, \code{ends_with()}, and \code{contains()} accept several patterns, e.g -\code{starts_with("Sep", "Petal")}. -\item or a function testing for logical conditions, e.g. \code{is.numeric()} (or +\code{starts_with("Sep", "Petal")}. \code{regex()} can be used to define regular +expression patterns. +\item a function testing for logical conditions, e.g. \code{is.numeric()} (or \code{is.numeric}), or any user-defined function that selects the variables for which the function returns \code{TRUE} (like: \code{foo <- function(x) mean(x) > 3}), \item ranges specified via literal variable names, select-helpers (except \code{regex()}) and (user-defined) functions can be negated, i.e. return -non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with("")}, +non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with()}, \code{-is.numeric} or \code{-(Sepal.Width:Petal.Length)}. \strong{Note:} Negation means that matches are \emph{excluded}, and thus, the \code{exclude} argument can be used alternatively. For instance, \code{select=-ends_with("Length")} (with @@ -133,7 +134,7 @@ treated as regular expression. When \code{regex = TRUE}, select \emph{must} be a character string (or a variable containing a character string) and is not allowed to be one of the supported select-helpers or a character vector of length > 1. \code{regex = TRUE} is comparable to using one of the two -select-helpers, \code{select = contains("")} or \code{select = regex("")}, however, +select-helpers, \code{select = contains()} or \code{select = regex()}, however, since the select-helpers may not work when called from inside other functions (see 'Details'), this argument may be used as workaround.} } diff --git a/man/extract_column_names.Rd b/man/extract_column_names.Rd index bb0f9ada8..fe334e22f 100644 --- a/man/extract_column_names.Rd +++ b/man/extract_column_names.Rd @@ -53,15 +53,16 @@ character vector of variable names including ranges specified via \code{:} \item a vector of negative integers, giving the positions counting from the right (e.g., \code{-1} or \code{-1:-3}), \item one of the following select-helpers: \code{starts_with()}, \code{ends_with()}, -\code{contains()}, a range using \code{:} or \code{regex("")}. \code{starts_with()}, +\code{contains()}, a range using \code{:}, or \code{regex()}. \code{starts_with()}, \code{ends_with()}, and \code{contains()} accept several patterns, e.g -\code{starts_with("Sep", "Petal")}. -\item or a function testing for logical conditions, e.g. \code{is.numeric()} (or +\code{starts_with("Sep", "Petal")}. \code{regex()} can be used to define regular +expression patterns. +\item a function testing for logical conditions, e.g. \code{is.numeric()} (or \code{is.numeric}), or any user-defined function that selects the variables for which the function returns \code{TRUE} (like: \code{foo <- function(x) mean(x) > 3}), \item ranges specified via literal variable names, select-helpers (except \code{regex()}) and (user-defined) functions can be negated, i.e. return -non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with("")}, +non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with()}, \code{-is.numeric} or \code{-(Sepal.Width:Petal.Length)}. \strong{Note:} Negation means that matches are \emph{excluded}, and thus, the \code{exclude} argument can be used alternatively. For instance, \code{select=-ends_with("Length")} (with @@ -86,7 +87,7 @@ treated as regular expression. When \code{regex = TRUE}, select \emph{must} be a character string (or a variable containing a character string) and is not allowed to be one of the supported select-helpers or a character vector of length > 1. \code{regex = TRUE} is comparable to using one of the two -select-helpers, \code{select = contains("")} or \code{select = regex("")}, however, +select-helpers, \code{select = contains()} or \code{select = regex()}, however, since the select-helpers may not work when called from inside other functions (see 'Details'), this argument may be used as workaround.} @@ -167,7 +168,10 @@ extract_column_names(iris, starts_with("Sepal"), exclude = contains("Width")) numeric_mean_35 <- function(x) is.numeric(x) && mean(x, na.rm = TRUE) > 3.5 extract_column_names(iris, numeric_mean_35) -# find range of colum names by range, using character vector +# find column names, using range +extract_column_names(mtcars, c(cyl:hp, wt)) + +# find range of column names by range, using character vector extract_column_names(mtcars, c("cyl:hp", "wt")) # rename returned columns for "data_select()" diff --git a/man/labels_to_levels.Rd b/man/labels_to_levels.Rd index 163eb0eaa..c75e2f537 100644 --- a/man/labels_to_levels.Rd +++ b/man/labels_to_levels.Rd @@ -43,15 +43,16 @@ character vector of variable names including ranges specified via \code{:} \item a vector of negative integers, giving the positions counting from the right (e.g., \code{-1} or \code{-1:-3}), \item one of the following select-helpers: \code{starts_with()}, \code{ends_with()}, -\code{contains()}, a range using \code{:} or \code{regex("")}. \code{starts_with()}, +\code{contains()}, a range using \code{:}, or \code{regex()}. \code{starts_with()}, \code{ends_with()}, and \code{contains()} accept several patterns, e.g -\code{starts_with("Sep", "Petal")}. -\item or a function testing for logical conditions, e.g. \code{is.numeric()} (or +\code{starts_with("Sep", "Petal")}. \code{regex()} can be used to define regular +expression patterns. +\item a function testing for logical conditions, e.g. \code{is.numeric()} (or \code{is.numeric}), or any user-defined function that selects the variables for which the function returns \code{TRUE} (like: \code{foo <- function(x) mean(x) > 3}), \item ranges specified via literal variable names, select-helpers (except \code{regex()}) and (user-defined) functions can be negated, i.e. return -non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with("")}, +non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with()}, \code{-is.numeric} or \code{-(Sepal.Width:Petal.Length)}. \strong{Note:} Negation means that matches are \emph{excluded}, and thus, the \code{exclude} argument can be used alternatively. For instance, \code{select=-ends_with("Length")} (with @@ -86,7 +87,7 @@ treated as regular expression. When \code{regex = TRUE}, select \emph{must} be a character string (or a variable containing a character string) and is not allowed to be one of the supported select-helpers or a character vector of length > 1. \code{regex = TRUE} is comparable to using one of the two -select-helpers, \code{select = contains("")} or \code{select = regex("")}, however, +select-helpers, \code{select = contains()} or \code{select = regex()}, however, since the select-helpers may not work when called from inside other functions (see 'Details'), this argument may be used as workaround.} } diff --git a/man/means_by_group.Rd b/man/means_by_group.Rd index 6c06ac3b1..8407263b7 100644 --- a/man/means_by_group.Rd +++ b/man/means_by_group.Rd @@ -61,15 +61,16 @@ character vector of variable names including ranges specified via \code{:} \item a vector of negative integers, giving the positions counting from the right (e.g., \code{-1} or \code{-1:-3}), \item one of the following select-helpers: \code{starts_with()}, \code{ends_with()}, -\code{contains()}, a range using \code{:} or \code{regex("")}. \code{starts_with()}, +\code{contains()}, a range using \code{:}, or \code{regex()}. \code{starts_with()}, \code{ends_with()}, and \code{contains()} accept several patterns, e.g -\code{starts_with("Sep", "Petal")}. -\item or a function testing for logical conditions, e.g. \code{is.numeric()} (or +\code{starts_with("Sep", "Petal")}. \code{regex()} can be used to define regular +expression patterns. +\item a function testing for logical conditions, e.g. \code{is.numeric()} (or \code{is.numeric}), or any user-defined function that selects the variables for which the function returns \code{TRUE} (like: \code{foo <- function(x) mean(x) > 3}), \item ranges specified via literal variable names, select-helpers (except \code{regex()}) and (user-defined) functions can be negated, i.e. return -non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with("")}, +non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with()}, \code{-is.numeric} or \code{-(Sepal.Width:Petal.Length)}. \strong{Note:} Negation means that matches are \emph{excluded}, and thus, the \code{exclude} argument can be used alternatively. For instance, \code{select=-ends_with("Length")} (with @@ -94,7 +95,7 @@ treated as regular expression. When \code{regex = TRUE}, select \emph{must} be a character string (or a variable containing a character string) and is not allowed to be one of the supported select-helpers or a character vector of length > 1. \code{regex = TRUE} is comparable to using one of the two -select-helpers, \code{select = contains("")} or \code{select = regex("")}, however, +select-helpers, \code{select = contains()} or \code{select = regex()}, however, since the select-helpers may not work when called from inside other functions (see 'Details'), this argument may be used as workaround.} diff --git a/man/normalize.Rd b/man/normalize.Rd index c325e98fe..ecb3a2706 100644 --- a/man/normalize.Rd +++ b/man/normalize.Rd @@ -81,15 +81,16 @@ character vector of variable names including ranges specified via \code{:} \item a vector of negative integers, giving the positions counting from the right (e.g., \code{-1} or \code{-1:-3}), \item one of the following select-helpers: \code{starts_with()}, \code{ends_with()}, -\code{contains()}, a range using \code{:} or \code{regex("")}. \code{starts_with()}, +\code{contains()}, a range using \code{:}, or \code{regex()}. \code{starts_with()}, \code{ends_with()}, and \code{contains()} accept several patterns, e.g -\code{starts_with("Sep", "Petal")}. -\item or a function testing for logical conditions, e.g. \code{is.numeric()} (or +\code{starts_with("Sep", "Petal")}. \code{regex()} can be used to define regular +expression patterns. +\item a function testing for logical conditions, e.g. \code{is.numeric()} (or \code{is.numeric}), or any user-defined function that selects the variables for which the function returns \code{TRUE} (like: \code{foo <- function(x) mean(x) > 3}), \item ranges specified via literal variable names, select-helpers (except \code{regex()}) and (user-defined) functions can be negated, i.e. return -non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with("")}, +non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with()}, \code{-is.numeric} or \code{-(Sepal.Width:Petal.Length)}. \strong{Note:} Negation means that matches are \emph{excluded}, and thus, the \code{exclude} argument can be used alternatively. For instance, \code{select=-ends_with("Length")} (with @@ -121,7 +122,7 @@ treated as regular expression. When \code{regex = TRUE}, select \emph{must} be a character string (or a variable containing a character string) and is not allowed to be one of the supported select-helpers or a character vector of length > 1. \code{regex = TRUE} is comparable to using one of the two -select-helpers, \code{select = contains("")} or \code{select = regex("")}, however, +select-helpers, \code{select = contains()} or \code{select = regex()}, however, since the select-helpers may not work when called from inside other functions (see 'Details'), this argument may be used as workaround.} } diff --git a/man/ranktransform.Rd b/man/ranktransform.Rd index 7046db2b5..44b0052c4 100644 --- a/man/ranktransform.Rd +++ b/man/ranktransform.Rd @@ -49,15 +49,16 @@ character vector of variable names including ranges specified via \code{:} \item a vector of negative integers, giving the positions counting from the right (e.g., \code{-1} or \code{-1:-3}), \item one of the following select-helpers: \code{starts_with()}, \code{ends_with()}, -\code{contains()}, a range using \code{:} or \code{regex("")}. \code{starts_with()}, +\code{contains()}, a range using \code{:}, or \code{regex()}. \code{starts_with()}, \code{ends_with()}, and \code{contains()} accept several patterns, e.g -\code{starts_with("Sep", "Petal")}. -\item or a function testing for logical conditions, e.g. \code{is.numeric()} (or +\code{starts_with("Sep", "Petal")}. \code{regex()} can be used to define regular +expression patterns. +\item a function testing for logical conditions, e.g. \code{is.numeric()} (or \code{is.numeric}), or any user-defined function that selects the variables for which the function returns \code{TRUE} (like: \code{foo <- function(x) mean(x) > 3}), \item ranges specified via literal variable names, select-helpers (except \code{regex()}) and (user-defined) functions can be negated, i.e. return -non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with("")}, +non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with()}, \code{-is.numeric} or \code{-(Sepal.Width:Petal.Length)}. \strong{Note:} Negation means that matches are \emph{excluded}, and thus, the \code{exclude} argument can be used alternatively. For instance, \code{select=-ends_with("Length")} (with @@ -82,7 +83,7 @@ treated as regular expression. When \code{regex = TRUE}, select \emph{must} be a character string (or a variable containing a character string) and is not allowed to be one of the supported select-helpers or a character vector of length > 1. \code{regex = TRUE} is comparable to using one of the two -select-helpers, \code{select = contains("")} or \code{select = regex("")}, however, +select-helpers, \code{select = contains()} or \code{select = regex()}, however, since the select-helpers may not work when called from inside other functions (see 'Details'), this argument may be used as workaround.} } diff --git a/man/recode_values.Rd b/man/recode_values.Rd index 66b7c6e32..11f9cfadc 100644 --- a/man/recode_values.Rd +++ b/man/recode_values.Rd @@ -70,15 +70,16 @@ character vector of variable names including ranges specified via \code{:} \item a vector of negative integers, giving the positions counting from the right (e.g., \code{-1} or \code{-1:-3}), \item one of the following select-helpers: \code{starts_with()}, \code{ends_with()}, -\code{contains()}, a range using \code{:} or \code{regex("")}. \code{starts_with()}, +\code{contains()}, a range using \code{:}, or \code{regex()}. \code{starts_with()}, \code{ends_with()}, and \code{contains()} accept several patterns, e.g -\code{starts_with("Sep", "Petal")}. -\item or a function testing for logical conditions, e.g. \code{is.numeric()} (or +\code{starts_with("Sep", "Petal")}. \code{regex()} can be used to define regular +expression patterns. +\item a function testing for logical conditions, e.g. \code{is.numeric()} (or \code{is.numeric}), or any user-defined function that selects the variables for which the function returns \code{TRUE} (like: \code{foo <- function(x) mean(x) > 3}), \item ranges specified via literal variable names, select-helpers (except \code{regex()}) and (user-defined) functions can be negated, i.e. return -non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with("")}, +non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with()}, \code{-is.numeric} or \code{-(Sepal.Width:Petal.Length)}. \strong{Note:} Negation means that matches are \emph{excluded}, and thus, the \code{exclude} argument can be used alternatively. For instance, \code{select=-ends_with("Length")} (with @@ -113,7 +114,7 @@ treated as regular expression. When \code{regex = TRUE}, select \emph{must} be a character string (or a variable containing a character string) and is not allowed to be one of the supported select-helpers or a character vector of length > 1. \code{regex = TRUE} is comparable to using one of the two -select-helpers, \code{select = contains("")} or \code{select = regex("")}, however, +select-helpers, \code{select = contains()} or \code{select = regex()}, however, since the select-helpers may not work when called from inside other functions (see 'Details'), this argument may be used as workaround.} } diff --git a/man/rescale.Rd b/man/rescale.Rd index 490964777..8d6025a8d 100644 --- a/man/rescale.Rd +++ b/man/rescale.Rd @@ -77,15 +77,16 @@ character vector of variable names including ranges specified via \code{:} \item a vector of negative integers, giving the positions counting from the right (e.g., \code{-1} or \code{-1:-3}), \item one of the following select-helpers: \code{starts_with()}, \code{ends_with()}, -\code{contains()}, a range using \code{:} or \code{regex("")}. \code{starts_with()}, +\code{contains()}, a range using \code{:}, or \code{regex()}. \code{starts_with()}, \code{ends_with()}, and \code{contains()} accept several patterns, e.g -\code{starts_with("Sep", "Petal")}. -\item or a function testing for logical conditions, e.g. \code{is.numeric()} (or +\code{starts_with("Sep", "Petal")}. \code{regex()} can be used to define regular +expression patterns. +\item a function testing for logical conditions, e.g. \code{is.numeric()} (or \code{is.numeric}), or any user-defined function that selects the variables for which the function returns \code{TRUE} (like: \code{foo <- function(x) mean(x) > 3}), \item ranges specified via literal variable names, select-helpers (except \code{regex()}) and (user-defined) functions can be negated, i.e. return -non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with("")}, +non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with()}, \code{-is.numeric} or \code{-(Sepal.Width:Petal.Length)}. \strong{Note:} Negation means that matches are \emph{excluded}, and thus, the \code{exclude} argument can be used alternatively. For instance, \code{select=-ends_with("Length")} (with @@ -120,7 +121,7 @@ treated as regular expression. When \code{regex = TRUE}, select \emph{must} be a character string (or a variable containing a character string) and is not allowed to be one of the supported select-helpers or a character vector of length > 1. \code{regex = TRUE} is comparable to using one of the two -select-helpers, \code{select = contains("")} or \code{select = regex("")}, however, +select-helpers, \code{select = contains()} or \code{select = regex()}, however, since the select-helpers may not work when called from inside other functions (see 'Details'), this argument may be used as workaround.} } diff --git a/man/reverse.Rd b/man/reverse.Rd index 5767908ff..0f33154a4 100644 --- a/man/reverse.Rd +++ b/man/reverse.Rd @@ -55,15 +55,16 @@ character vector of variable names including ranges specified via \code{:} \item a vector of negative integers, giving the positions counting from the right (e.g., \code{-1} or \code{-1:-3}), \item one of the following select-helpers: \code{starts_with()}, \code{ends_with()}, -\code{contains()}, a range using \code{:} or \code{regex("")}. \code{starts_with()}, +\code{contains()}, a range using \code{:}, or \code{regex()}. \code{starts_with()}, \code{ends_with()}, and \code{contains()} accept several patterns, e.g -\code{starts_with("Sep", "Petal")}. -\item or a function testing for logical conditions, e.g. \code{is.numeric()} (or +\code{starts_with("Sep", "Petal")}. \code{regex()} can be used to define regular +expression patterns. +\item a function testing for logical conditions, e.g. \code{is.numeric()} (or \code{is.numeric}), or any user-defined function that selects the variables for which the function returns \code{TRUE} (like: \code{foo <- function(x) mean(x) > 3}), \item ranges specified via literal variable names, select-helpers (except \code{regex()}) and (user-defined) functions can be negated, i.e. return -non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with("")}, +non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with()}, \code{-is.numeric} or \code{-(Sepal.Width:Petal.Length)}. \strong{Note:} Negation means that matches are \emph{excluded}, and thus, the \code{exclude} argument can be used alternatively. For instance, \code{select=-ends_with("Length")} (with @@ -98,7 +99,7 @@ treated as regular expression. When \code{regex = TRUE}, select \emph{must} be a character string (or a variable containing a character string) and is not allowed to be one of the supported select-helpers or a character vector of length > 1. \code{regex = TRUE} is comparable to using one of the two -select-helpers, \code{select = contains("")} or \code{select = regex("")}, however, +select-helpers, \code{select = contains()} or \code{select = regex()}, however, since the select-helpers may not work when called from inside other functions (see 'Details'), this argument may be used as workaround.} } diff --git a/man/row_count.Rd b/man/row_count.Rd index 7bf54fe5f..1a87de609 100644 --- a/man/row_count.Rd +++ b/man/row_count.Rd @@ -33,15 +33,16 @@ character vector of variable names including ranges specified via \code{:} \item a vector of negative integers, giving the positions counting from the right (e.g., \code{-1} or \code{-1:-3}), \item one of the following select-helpers: \code{starts_with()}, \code{ends_with()}, -\code{contains()}, a range using \code{:} or \code{regex("")}. \code{starts_with()}, +\code{contains()}, a range using \code{:}, or \code{regex()}. \code{starts_with()}, \code{ends_with()}, and \code{contains()} accept several patterns, e.g -\code{starts_with("Sep", "Petal")}. -\item or a function testing for logical conditions, e.g. \code{is.numeric()} (or +\code{starts_with("Sep", "Petal")}. \code{regex()} can be used to define regular +expression patterns. +\item a function testing for logical conditions, e.g. \code{is.numeric()} (or \code{is.numeric}), or any user-defined function that selects the variables for which the function returns \code{TRUE} (like: \code{foo <- function(x) mean(x) > 3}), \item ranges specified via literal variable names, select-helpers (except \code{regex()}) and (user-defined) functions can be negated, i.e. return -non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with("")}, +non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with()}, \code{-is.numeric} or \code{-(Sepal.Width:Petal.Length)}. \strong{Note:} Negation means that matches are \emph{excluded}, and thus, the \code{exclude} argument can be used alternatively. For instance, \code{select=-ends_with("Length")} (with @@ -76,7 +77,7 @@ treated as regular expression. When \code{regex = TRUE}, select \emph{must} be a character string (or a variable containing a character string) and is not allowed to be one of the supported select-helpers or a character vector of length > 1. \code{regex = TRUE} is comparable to using one of the two -select-helpers, \code{select = contains("")} or \code{select = regex("")}, however, +select-helpers, \code{select = contains()} or \code{select = regex()}, however, since the select-helpers may not work when called from inside other functions (see 'Details'), this argument may be used as workaround.} diff --git a/man/row_means.Rd b/man/row_means.Rd index 43d85b5b0..3c859e992 100644 --- a/man/row_means.Rd +++ b/man/row_means.Rd @@ -47,15 +47,16 @@ character vector of variable names including ranges specified via \code{:} \item a vector of negative integers, giving the positions counting from the right (e.g., \code{-1} or \code{-1:-3}), \item one of the following select-helpers: \code{starts_with()}, \code{ends_with()}, -\code{contains()}, a range using \code{:} or \code{regex("")}. \code{starts_with()}, +\code{contains()}, a range using \code{:}, or \code{regex()}. \code{starts_with()}, \code{ends_with()}, and \code{contains()} accept several patterns, e.g -\code{starts_with("Sep", "Petal")}. -\item or a function testing for logical conditions, e.g. \code{is.numeric()} (or +\code{starts_with("Sep", "Petal")}. \code{regex()} can be used to define regular +expression patterns. +\item a function testing for logical conditions, e.g. \code{is.numeric()} (or \code{is.numeric}), or any user-defined function that selects the variables for which the function returns \code{TRUE} (like: \code{foo <- function(x) mean(x) > 3}), \item ranges specified via literal variable names, select-helpers (except \code{regex()}) and (user-defined) functions can be negated, i.e. return -non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with("")}, +non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with()}, \code{-is.numeric} or \code{-(Sepal.Width:Petal.Length)}. \strong{Note:} Negation means that matches are \emph{excluded}, and thus, the \code{exclude} argument can be used alternatively. For instance, \code{select=-ends_with("Length")} (with @@ -95,7 +96,7 @@ treated as regular expression. When \code{regex = TRUE}, select \emph{must} be a character string (or a variable containing a character string) and is not allowed to be one of the supported select-helpers or a character vector of length > 1. \code{regex = TRUE} is comparable to using one of the two -select-helpers, \code{select = contains("")} or \code{select = regex("")}, however, +select-helpers, \code{select = contains()} or \code{select = regex()}, however, since the select-helpers may not work when called from inside other functions (see 'Details'), this argument may be used as workaround.} diff --git a/man/slide.Rd b/man/slide.Rd index 2950855f4..d68f7e30a 100644 --- a/man/slide.Rd +++ b/man/slide.Rd @@ -44,15 +44,16 @@ character vector of variable names including ranges specified via \code{:} \item a vector of negative integers, giving the positions counting from the right (e.g., \code{-1} or \code{-1:-3}), \item one of the following select-helpers: \code{starts_with()}, \code{ends_with()}, -\code{contains()}, a range using \code{:} or \code{regex("")}. \code{starts_with()}, +\code{contains()}, a range using \code{:}, or \code{regex()}. \code{starts_with()}, \code{ends_with()}, and \code{contains()} accept several patterns, e.g -\code{starts_with("Sep", "Petal")}. -\item or a function testing for logical conditions, e.g. \code{is.numeric()} (or +\code{starts_with("Sep", "Petal")}. \code{regex()} can be used to define regular +expression patterns. +\item a function testing for logical conditions, e.g. \code{is.numeric()} (or \code{is.numeric}), or any user-defined function that selects the variables for which the function returns \code{TRUE} (like: \code{foo <- function(x) mean(x) > 3}), \item ranges specified via literal variable names, select-helpers (except \code{regex()}) and (user-defined) functions can be negated, i.e. return -non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with("")}, +non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with()}, \code{-is.numeric} or \code{-(Sepal.Width:Petal.Length)}. \strong{Note:} Negation means that matches are \emph{excluded}, and thus, the \code{exclude} argument can be used alternatively. For instance, \code{select=-ends_with("Length")} (with @@ -87,7 +88,7 @@ treated as regular expression. When \code{regex = TRUE}, select \emph{must} be a character string (or a variable containing a character string) and is not allowed to be one of the supported select-helpers or a character vector of length > 1. \code{regex = TRUE} is comparable to using one of the two -select-helpers, \code{select = contains("")} or \code{select = regex("")}, however, +select-helpers, \code{select = contains()} or \code{select = regex()}, however, since the select-helpers may not work when called from inside other functions (see 'Details'), this argument may be used as workaround.} diff --git a/man/standardize.Rd b/man/standardize.Rd index fcc8c6ae7..8e8d66867 100644 --- a/man/standardize.Rd +++ b/man/standardize.Rd @@ -155,15 +155,16 @@ character vector of variable names including ranges specified via \code{:} \item a vector of negative integers, giving the positions counting from the right (e.g., \code{-1} or \code{-1:-3}), \item one of the following select-helpers: \code{starts_with()}, \code{ends_with()}, -\code{contains()}, a range using \code{:} or \code{regex("")}. \code{starts_with()}, +\code{contains()}, a range using \code{:}, or \code{regex()}. \code{starts_with()}, \code{ends_with()}, and \code{contains()} accept several patterns, e.g -\code{starts_with("Sep", "Petal")}. -\item or a function testing for logical conditions, e.g. \code{is.numeric()} (or +\code{starts_with("Sep", "Petal")}. \code{regex()} can be used to define regular +expression patterns. +\item a function testing for logical conditions, e.g. \code{is.numeric()} (or \code{is.numeric}), or any user-defined function that selects the variables for which the function returns \code{TRUE} (like: \code{foo <- function(x) mean(x) > 3}), \item ranges specified via literal variable names, select-helpers (except \code{regex()}) and (user-defined) functions can be negated, i.e. return -non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with("")}, +non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with()}, \code{-is.numeric} or \code{-(Sepal.Width:Petal.Length)}. \strong{Note:} Negation means that matches are \emph{excluded}, and thus, the \code{exclude} argument can be used alternatively. For instance, \code{select=-ends_with("Length")} (with @@ -201,7 +202,7 @@ treated as regular expression. When \code{regex = TRUE}, select \emph{must} be a character string (or a variable containing a character string) and is not allowed to be one of the supported select-helpers or a character vector of length > 1. \code{regex = TRUE} is comparable to using one of the two -select-helpers, \code{select = contains("")} or \code{select = regex("")}, however, +select-helpers, \code{select = contains()} or \code{select = regex()}, however, since the select-helpers may not work when called from inside other functions (see 'Details'), this argument may be used as workaround.} } diff --git a/man/to_factor.Rd b/man/to_factor.Rd index d544bdaae..7a500c071 100644 --- a/man/to_factor.Rd +++ b/man/to_factor.Rd @@ -46,15 +46,16 @@ character vector of variable names including ranges specified via \code{:} \item a vector of negative integers, giving the positions counting from the right (e.g., \code{-1} or \code{-1:-3}), \item one of the following select-helpers: \code{starts_with()}, \code{ends_with()}, -\code{contains()}, a range using \code{:} or \code{regex("")}. \code{starts_with()}, +\code{contains()}, a range using \code{:}, or \code{regex()}. \code{starts_with()}, \code{ends_with()}, and \code{contains()} accept several patterns, e.g -\code{starts_with("Sep", "Petal")}. -\item or a function testing for logical conditions, e.g. \code{is.numeric()} (or +\code{starts_with("Sep", "Petal")}. \code{regex()} can be used to define regular +expression patterns. +\item a function testing for logical conditions, e.g. \code{is.numeric()} (or \code{is.numeric}), or any user-defined function that selects the variables for which the function returns \code{TRUE} (like: \code{foo <- function(x) mean(x) > 3}), \item ranges specified via literal variable names, select-helpers (except \code{regex()}) and (user-defined) functions can be negated, i.e. return -non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with("")}, +non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with()}, \code{-is.numeric} or \code{-(Sepal.Width:Petal.Length)}. \strong{Note:} Negation means that matches are \emph{excluded}, and thus, the \code{exclude} argument can be used alternatively. For instance, \code{select=-ends_with("Length")} (with @@ -89,7 +90,7 @@ treated as regular expression. When \code{regex = TRUE}, select \emph{must} be a character string (or a variable containing a character string) and is not allowed to be one of the supported select-helpers or a character vector of length > 1. \code{regex = TRUE} is comparable to using one of the two -select-helpers, \code{select = contains("")} or \code{select = regex("")}, however, +select-helpers, \code{select = contains()} or \code{select = regex()}, however, since the select-helpers may not work when called from inside other functions (see 'Details'), this argument may be used as workaround.} } diff --git a/man/to_numeric.Rd b/man/to_numeric.Rd index 39f04c3a9..abfcec0d8 100644 --- a/man/to_numeric.Rd +++ b/man/to_numeric.Rd @@ -40,15 +40,16 @@ character vector of variable names including ranges specified via \code{:} \item a vector of negative integers, giving the positions counting from the right (e.g., \code{-1} or \code{-1:-3}), \item one of the following select-helpers: \code{starts_with()}, \code{ends_with()}, -\code{contains()}, a range using \code{:} or \code{regex("")}. \code{starts_with()}, +\code{contains()}, a range using \code{:}, or \code{regex()}. \code{starts_with()}, \code{ends_with()}, and \code{contains()} accept several patterns, e.g -\code{starts_with("Sep", "Petal")}. -\item or a function testing for logical conditions, e.g. \code{is.numeric()} (or +\code{starts_with("Sep", "Petal")}. \code{regex()} can be used to define regular +expression patterns. +\item a function testing for logical conditions, e.g. \code{is.numeric()} (or \code{is.numeric}), or any user-defined function that selects the variables for which the function returns \code{TRUE} (like: \code{foo <- function(x) mean(x) > 3}), \item ranges specified via literal variable names, select-helpers (except \code{regex()}) and (user-defined) functions can be negated, i.e. return -non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with("")}, +non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with()}, \code{-is.numeric} or \code{-(Sepal.Width:Petal.Length)}. \strong{Note:} Negation means that matches are \emph{excluded}, and thus, the \code{exclude} argument can be used alternatively. For instance, \code{select=-ends_with("Length")} (with @@ -94,7 +95,7 @@ treated as regular expression. When \code{regex = TRUE}, select \emph{must} be a character string (or a variable containing a character string) and is not allowed to be one of the supported select-helpers or a character vector of length > 1. \code{regex = TRUE} is comparable to using one of the two -select-helpers, \code{select = contains("")} or \code{select = regex("")}, however, +select-helpers, \code{select = contains()} or \code{select = regex()}, however, since the select-helpers may not work when called from inside other functions (see 'Details'), this argument may be used as workaround.} From 5463c93e0ae2954c95eeee4b42d27fbdf6f30fdb Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 27 Nov 2024 17:43:13 +0100 Subject: [PATCH 4/8] `data_summary()` works with `bayestestR::ci()` (#483) * Draft new `data_summary()` function * check if we can avoid duplicated code * pkgdown * fix * fixes * lintr * add tests * code style * desc, news * fix * add print method and snapshot test * add test * correct english form * test * include NA, sort output * add test * meaningful code comments * add test * Update data_summary.R * fix --- DESCRIPTION | 2 +- NEWS.md | 3 +++ R/data_summary.R | 21 ++++++++++++++++----- tests/testthat/test-data_summary.R | 23 +++++++++++++++++++++++ 4 files changed, 43 insertions(+), 6 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index be41e0f6f..b75f96278 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: datawizard Title: Easy Data Wrangling and Statistical Transformations -Version: 0.13.0.14 +Version: 0.13.0.15 Authors@R: c( person("Indrajeet", "Patil", , "patilindrajeet.science@gmail.com", role = "aut", comment = c(ORCID = "0000-0003-1995-6531")), diff --git a/NEWS.md b/NEWS.md index 15b7f853d..a701ba2b8 100644 --- a/NEWS.md +++ b/NEWS.md @@ -25,6 +25,9 @@ CHANGES * The `replacement` argument in `data_rename()` now supports glue-styled tokens (#563). +* `data_summary()` also accepts the results of `bayestestR::ci()` as summary + function (#483). + BUG FIXES * `describe_distribution()` no longer errors if the sample was too sparse to compute diff --git a/R/data_summary.R b/R/data_summary.R index 546c47659..521266d61 100644 --- a/R/data_summary.R +++ b/R/data_summary.R @@ -122,7 +122,7 @@ data_summary.data.frame <- function(x, ..., by = NULL, remove_na = FALSE) { # bind grouping-variables and values summarised_data <- cbind(s[1, by], summarised_data) # make sure we have proper column names - colnames(summarised_data) <- c(by, vapply(summarise, names, character(1))) + colnames(summarised_data) <- c(by, unlist(lapply(summarise, names))) summarised_data }) out <- do.call(rbind, out) @@ -187,18 +187,24 @@ data_summary.grouped_df <- function(x, ..., by = NULL, remove_na = FALSE) { out <- lapply(seq_along(dots), function(i) { new_variable <- .get_new_dots_variable(dots, i, data) - stats::setNames(new_variable, names(dots)[i]) + if (inherits(new_variable, c("bayestestR_ci", "bayestestR_eti"))) { + stats::setNames(new_variable, c("CI", "CI_low", "CI_high")) + } else { + stats::setNames(new_variable, names(dots)[i]) + } }) } # check for correct length of output - must be a single value! - if (any(lengths(out) != 1)) { + # Exception: bayestestR::ci() + wrong_length <- !sapply(out, inherits, what = c("bayestestR_ci", "bayestestR_eti")) & lengths(out) != 1 # nolint + if (any(wrong_length)) { insight::format_error( paste0( "Each expression must return a single value. Following expression", - ifelse(sum(lengths(out) != 1) > 1, "s", " "), + ifelse(sum(wrong_length) > 1, "s", " "), " returned more than one value: ", - text_concatenate(vapply(dots[lengths(out) != 1], insight::safe_deparse, character(1)), enclose = "\"") + text_concatenate(vapply(dots[wrong_length], insight::safe_deparse, character(1)), enclose = "\"") ) ) } @@ -214,6 +220,11 @@ print.dw_data_summary <- function(x, ...) { if (nrow(x) == 0) { cat("No matches found.\n") } else { + if (all(c("CI", "CI_low", "CI_high") %in% colnames(x))) { + ci <- insight::format_table(x[c("CI", "CI_low", "CI_high")], ...) + x$CI <- x$CI_low <- x$CI_high <- NULL + x <- cbind(x, ci) + } cat(insight::export_table(x, missing = "", ...)) } } diff --git a/tests/testthat/test-data_summary.R b/tests/testthat/test-data_summary.R index c60b142d2..701fc996b 100644 --- a/tests/testthat/test-data_summary.R +++ b/tests/testthat/test-data_summary.R @@ -228,3 +228,26 @@ test_that("data_summary, extra functions", { out <- data_summary(mtcars, n = n(), by = c("am", "gear")) expect_identical(out$n, c(15L, 4L, 8L, 5L)) }) + + +test_that("data_summary, bayestestR::ci", { + skip_if_not_installed("bayestesR") + data(mtcars) + out <- data_summary( + mtcars, + mean_value = mean(mpg), + ci = bayestestR::ci(mpg), + by = c("am", "gear") + ) + expect_snapshot(out) + expect_error( + data_summary( + mtcars, + mw = mean(mpg), + test = bayestestR::ci(mpg), + yolo = c(mean(mpg), sd(mpg)), + by = c("am", "gear") + ), + regex = "Each expression" + ) +}) From 81dd0e0abb9589987790d25b0707cba748ae29f6 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sun, 1 Dec 2024 10:51:31 +0100 Subject: [PATCH 5/8] Update `DESCRIPTION` to use latest 'easystats' dependencies (#569) [create-pull-request] automated change Co-authored-by: IndrajeetPatil <11330453+IndrajeetPatil@users.noreply.github.com> --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index b75f96278..bcb41528b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -33,7 +33,7 @@ BugReports: https://github.com/easystats/datawizard/issues Depends: R (>= 3.6) Imports: - insight (>= 0.20.5), + insight (>= 1.0.0), stats, utils Suggests: From 051221249c09717478e3bf119a212c070a53f2b2 Mon Sep 17 00:00:00 2001 From: Etienne Bacher <52219252+etiennebacher@users.noreply.github.com> Date: Mon, 2 Dec 2024 11:45:42 +0100 Subject: [PATCH 6/8] Revamp `data_rename()` (#568) * init * update wordlist * redoc * fix vignette * doc for text_format * comments * pkgdown * lints * seealso * pkgdown * do not use column indices as replacement * forbid partially named vector * add arg ifnotfound in select_nse() * simplify * simplify again [skip ci] * fix tests and examples * lint * use dev styler * fix --- DESCRIPTION | 2 +- NEWS.md | 17 +++- R/data_addprefix.R | 12 ++- R/data_rename.R | 143 +++++++++++++------------- R/select_nse.R | 151 +++++++++++++++++----------- R/text_format.R | 2 +- inst/WORDLIST | 13 +-- man/categorize.Rd | 3 +- man/data_match.Rd | 3 +- man/data_merge.Rd | 3 +- man/data_partition.Rd | 3 +- man/data_prefix_suffix.Rd | 103 +++++++++++++++++++ man/data_relocate.Rd | 3 +- man/data_rename.Rd | 116 ++++++--------------- man/data_rotate.Rd | 3 +- man/data_to_long.Rd | 3 +- man/data_to_wide.Rd | 3 +- man/extract_column_names.Rd | 3 +- man/recode_values.Rd | 3 +- man/rescale.Rd | 4 + man/slide.Rd | 3 +- man/text_format.Rd | 12 +-- man/winsorize.Rd | 3 +- pkgdown/_pkgdown.yaml | 1 + tests/testthat/test-attributes.R | 2 +- tests/testthat/test-data_rename.R | 107 ++++++++++---------- vignettes/tidyverse_translation.Rmd | 4 +- 27 files changed, 395 insertions(+), 330 deletions(-) create mode 100644 man/data_prefix_suffix.Rd diff --git a/DESCRIPTION b/DESCRIPTION index bcb41528b..a6869430a 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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", , "patilindrajeet.science@gmail.com", role = "aut", comment = c(ORCID = "0000-0003-1995-6531")), diff --git a/NEWS.md b/NEWS.md index a701ba2b8..eccbbe98d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,9 +1,18 @@ # datawizard (development) -BREAKING CHANGES - -* Argument `drop_na` in `data_match()` is deprecated now. Please use `remove_na` - instead. +BREAKING CHANGES AND DEPRECATIONS + +* Argument `drop_na` in `data_match()` is deprecated now. Please use + `remove_na` instead. + +* In `data_rename()` (#567): + - argument `pattern` is deprecated. Use `select` instead. + - argument `safe` is deprecated. The function now errors when `select` + contains unknown column names. + - when `replacement` is `NULL`, an error is now thrown (previously, column + indices were used as new names). + - if `select` (previously `pattern`) is a named vector, then all elements + must be named, e.g. `c(length = "Sepal.Length", "Sepal.Width")` errors. CHANGES diff --git a/R/data_addprefix.R b/R/data_addprefix.R index 6cf292ecc..9c20e6d71 100644 --- a/R/data_addprefix.R +++ b/R/data_addprefix.R @@ -1,5 +1,13 @@ -#' @rdname data_rename +#' Add a prefix or suffix to column names +#' +#' @rdname data_prefix_suffix #' @inheritParams extract_column_names +#' @param pattern A character string, which will be added as prefix or suffix +#' to the column names. +#' @param ... Other arguments passed to or from other functions. +#' +#' @seealso +#' [data_rename()] for more fine-grained column renaming. #' @examples #' # Add prefix / suffix to all columns #' head(data_addprefix(iris, "NEW_")) @@ -29,7 +37,7 @@ data_addprefix <- function(data, } -#' @rdname data_rename +#' @rdname data_prefix_suffix #' @export data_addsuffix <- function(data, pattern, diff --git a/R/data_rename.R b/R/data_rename.R index f5d6e0e03..eb2415d67 100644 --- a/R/data_rename.R +++ b/R/data_rename.R @@ -1,36 +1,27 @@ #' @title Rename columns and variable names -#' @name data_rename #' #' @description Safe and intuitive functions to rename variables or rows in #' data frames. `data_rename()` will rename column names, i.e. it facilitates -#' renaming variables `data_addprefix()` or `data_addsuffix()` add prefixes -#' or suffixes to column names. `data_rename_rows()` is a convenient shortcut +#' renaming variables. `data_rename_rows()` is a convenient shortcut #' to add or rename row names of a data frame, but unlike `row.names()`, its -#' input and output is a data frame, thus, integrating smoothly into a possible -#' pipe-workflow. +#' input and output is a data frame, thus, integrating smoothly into a +#' possible pipe-workflow. #' +#' @inheritParams extract_column_names #' @param data A data frame, or an object that can be coerced to a data frame. -#' @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 -#' 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 ` = ""` 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. -#' - `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: +#' in `select`. `select` and `replacement` must be of the same length. +#' - 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. +#' - `{letter}` will be replaced by alphabetical letters in sequential +#' order. #' If more than 26 letters are required, letters are repeated, but have -#' sequential numeric indices (e.g., `a1` to `z1`, followed by `a2` to `z2`). +#' sequential numeric indices (e.g., `a1` to `z1`, followed by `a2` to +#' `z2`). #' - Finally, the name of a user-defined object that is available in the #' environment can be used. Note that the object's name is not allowed to #' be one of the pre-defined tokens, `"col"`, `"n"` and `"letter"`. @@ -39,35 +30,32 @@ #' ```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 safe Deprecated. Passing unknown column names now always errors. +#' @param pattern Deprecated. Use `select` instead. #' @param ... Other arguments passed to or from other functions. #' +#' @details +#' `select` can also be a named character vector. In this case, the names are +#' used to rename the columns in the output data frame. See 'Examples'. +#' #' @return A modified data frame. #' #' @examples #' # Rename columns #' head(data_rename(iris, "Sepal.Length", "length")) -#' # data_rename(iris, "FakeCol", "length", safe=FALSE) # This fails -#' 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)) -#' #' # Change all #' head(data_rename(iris, replacement = paste0("Var", 1:5))) #' @@ -80,8 +68,7 @@ #' x <- c("hi", "there", "!") #' head(data_rename(mtcars[1:3], c("mpg", "cyl", "disp"), "col_{x}")) #' @seealso -#' - Functions to rename stuff: [data_rename()], [data_rename_rows()], -#' [data_addprefix()], [data_addsuffix()] +#' - Add a prefix or suffix to column names: [data_addprefix()], [data_addsuffix()] #' - Functions to reorder or remove columns: [data_reorder()], [data_relocate()], #' [data_remove()] #' - Functions to reshape, pivot or rotate data frames: [data_to_long()], @@ -96,28 +83,48 @@ #' #' @export data_rename <- function(data, - pattern = NULL, + select = NULL, replacement = NULL, safe = TRUE, verbose = TRUE, + pattern = NULL, ...) { - # change all names if no pattern specified - if (is.null(pattern)) { - pattern <- names(data) + # If the user does data_rename(iris, pattern = "Sepal.Length", "length"), + # then "length" is matched to select by position while it's the replacement + # => do the switch manually + if (!is.null(pattern)) { + .is_deprecated("pattern", "select") + if (!is.null(select)) { + replacement <- select + } + select <- pattern } - - if (!is.character(pattern)) { - insight::format_error("Argument `pattern` must be of type character.") + if (isFALSE(safe)) { + insight::format_warning("In `data_rename()`, argument `safe` is no longer used and will be removed in a future release.") # nolint } - # check if `pattern` has names, and if so, use as "replacement" - if (!is.null(names(pattern))) { - replacement <- names(pattern) + # change all names if no pattern specified + select <- .select_nse( + select, + data, + exclude = NULL, + ignore_case = NULL, + regex = NULL, + allow_rename = TRUE, + verbose = verbose, + ifnotfound = "error" + ) + + # Forbid partially named "select", + # Ex: if select = c("foo" = "Species", "Sepal.Length") then the 2nd name and + # 2nd value are "Sepal.Length" + if (!is.null(names(select)) && any(names(select) == select)) { + insight::format_error("When `select` is a named vector, all elements must be named.") } - # name columns 1, 2, 3 etc. if no replacement - if (is.null(replacement)) { - replacement <- paste0(seq_along(pattern)) + # check if `select` has names, and if so, use as "replacement" + if (!is.null(names(select))) { + replacement <- names(select) } # coerce to character @@ -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]) ) ) } @@ -163,30 +170,20 @@ 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) { - 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." - ) - ) - } else if (length(replacement) < length(pattern) && 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." - ) - ) + if (length(replacement) > length(select)) { + insight::format_error("There are more names in `replacement` than in `select`.") + } else if (length(replacement) < length(select) && !glue_style) { + insight::format_error("There are more names in `select` than in `replacement`") } - # 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) } } diff --git a/R/select_nse.R b/R/select_nse.R index 5120691a9..2d0255d0a 100644 --- a/R/select_nse.R +++ b/R/select_nse.R @@ -3,7 +3,7 @@ .select_nse <- function(select, data, exclude, ignore_case, regex = FALSE, remove_group_var = FALSE, allow_rename = FALSE, - verbose = FALSE) { + verbose = FALSE, ifnotfound = "warn") { .check_data(data) columns <- colnames(data) @@ -38,14 +38,16 @@ data, ignore_case = ignore_case, regex = regex, - verbose = verbose + verbose = verbose, + ifnotfound = ifnotfound ) excluded <- .eval_expr( expr_exclude, data, ignore_case = ignore_case, regex = regex, - verbose = verbose + verbose = verbose, + ifnotfound = ifnotfound ) selected_has_mix_idx <- any(selected < 0L) && any(selected > 0L) @@ -113,7 +115,7 @@ # * cyl:gear -> function (`:`) so find which function it is, then get the # position for each variable, then evaluate the function with the positions -.eval_expr <- function(x, data, ignore_case, regex, verbose) { +.eval_expr <- function(x, data, ignore_case, regex, verbose, ifnotfound) { if (is.null(x)) { return(NULL) } @@ -123,9 +125,18 @@ out <- switch(type, integer = x, double = as.integer(x), - character = .select_char(data, x, ignore_case, regex = regex, verbose), - symbol = .select_symbol(data, x, ignore_case, regex = regex, verbose), - language = .eval_call(data, x, ignore_case, regex = regex, verbose), + character = .select_char( + data, x, ignore_case, + regex = regex, verbose, ifnotfound + ), + symbol = .select_symbol( + data, x, ignore_case, + regex = regex, verbose, ifnotfound + ), + language = .eval_call( + data, x, ignore_case, + regex = regex, verbose, ifnotfound + ), insight::format_error(paste0( "Expressions of type <", typeof(x), "> cannot be evaluated for use when subsetting." @@ -143,7 +154,7 @@ # - character that should be regex-ed on variable names # - special word "all" to return all vars -.select_char <- function(data, x, ignore_case, regex, verbose) { +.select_char <- function(data, x, ignore_case, regex, verbose, ifnotfound) { # use colnames because names() doesn't work for matrices columns <- colnames(data) if (isTRUE(regex)) { @@ -160,7 +171,7 @@ colon_vars <- unlist(strsplit(x, ":", fixed = TRUE)) colon_match <- match(colon_vars, columns) if (anyNA(colon_match)) { - .warn_not_found(colon_vars, columns, colon_match, verbose) + .action_if_not_found(colon_vars, columns, colon_match, verbose, ifnotfound) matches <- NA } else { start_pos <- match(colon_vars[1], columns) @@ -180,26 +191,34 @@ # find columns, case sensitive matches <- match(x, columns) if (anyNA(matches)) { - .warn_not_found(x, columns, matches, verbose) + .action_if_not_found(x, columns, matches, verbose, ifnotfound) } matches[!is.na(matches)] } } # small helper, to avoid duplicated code -.warn_not_found <- function(x, columns, matches, verbose = TRUE) { - if (verbose) { - insight::format_warning( - paste0( - "Following variable(s) were not found: ", - toString(x[is.na(matches)]) - ), - .misspelled_string( - columns, - x[is.na(matches)], - default_message = "Possibly misspelled?" - ) - ) +.action_if_not_found <- function( + x, + columns, + matches, + verbose, + ifnotfound +) { + msg <- paste0( + "Following variable(s) were not found: ", + toString(x[is.na(matches)]) + ) + msg2 <- .misspelled_string( + columns, + x[is.na(matches)], + default_message = "Possibly misspelled?" + ) + if (ifnotfound == "error") { + insight::format_error(msg, msg2) + } + if (ifnotfound == "warn" && verbose) { + insight::format_warning(msg, msg2) } } @@ -217,7 +236,7 @@ # value but it errors because the function doesn't exist then it means that # it is a select helper that we grab from the error message. -.select_symbol <- function(data, x, ignore_case, regex, verbose) { +.select_symbol <- function(data, x, ignore_case, regex, verbose, ifnotfound) { try_eval <- try(eval(x), silent = TRUE) x_dep <- insight::safe_deparse(x) is_select_helper <- FALSE @@ -300,54 +319,59 @@ # Dispatch expressions to various select helpers according to the function call. -.eval_call <- function(data, x, ignore_case, regex, verbose) { +.eval_call <- function(data, x, ignore_case, regex, verbose, ifnotfound) { type <- insight::safe_deparse(x[[1]]) switch(type, - `:` = .select_seq(x, data, ignore_case, regex, verbose), - `-` = .select_minus(x, data, ignore_case, regex, verbose), - `c` = .select_c(x, data, ignore_case, regex, verbose), # nolint - `(` = .select_bracket(x, data, ignore_case, regex, verbose), - `[` = .select_square_bracket(x, data, ignore_case, regex, verbose), - `$` = .select_dollar(x, data, ignore_case, regex, verbose), - `~` = .select_tilde(x, data, ignore_case, regex, verbose), - list = .select_list(x, data, ignore_case, regex, verbose), - names = .select_names(x, data, ignore_case, regex, verbose), + `:` = .select_seq(x, data, ignore_case, regex, verbose, ifnotfound), + `-` = .select_minus(x, data, ignore_case, regex, verbose, ifnotfound), + `c` = .select_c(x, data, ignore_case, regex, verbose, ifnotfound), # nolint + `(` = .select_bracket(x, data, ignore_case, regex, verbose, ifnotfound), + `[` = .select_square_bracket( + x, data, ignore_case, regex, verbose, ifnotfound + ), + `$` = .select_dollar(x, data, ignore_case, regex, verbose, ifnotfound), + `~` = .select_tilde(x, data, ignore_case, regex, verbose, ifnotfound), + list = .select_list(x, data, ignore_case, regex, verbose, ifnotfound), + names = .select_names(x, data, ignore_case, regex, verbose, ifnotfound), starts_with = , ends_with = , matches = , contains = , - regex = .select_helper(x, data, ignore_case, regex, verbose), - .select_context(x, data, ignore_case, regex, verbose) + regex = .select_helper(x, data, ignore_case, regex, verbose, ifnotfound), + .select_context(x, data, ignore_case, regex, verbose, ifnotfound) ) } # e.g 1:3, or gear:cyl -.select_seq <- function(expr, data, ignore_case, regex, verbose) { +.select_seq <- function(expr, data, ignore_case, regex, verbose, ifnotfound) { x <- .eval_expr( expr[[2]], data = data, ignore_case = ignore_case, regex = regex, - verbose = verbose + verbose = verbose, + ifnotfound = ifnotfound ) y <- .eval_expr( expr[[3]], data = data, ignore_case = ignore_case, regex = regex, - verbose = verbose + verbose = verbose, + ifnotfound = ifnotfound ) x:y } # e.g -cyl -.select_minus <- function(expr, data, ignore_case, regex, verbose) { +.select_minus <- function(expr, data, ignore_case, regex, verbose, ifnotfound) { x <- .eval_expr( expr[[2]], data, ignore_case = ignore_case, regex = regex, - verbose = verbose + verbose = verbose, + ifnotfound = ifnotfound ) if (length(x) == 0L) { seq_along(data) @@ -357,7 +381,7 @@ } # e.g c("gear", "cyl") -.select_c <- function(expr, data, ignore_case, regex, verbose) { +.select_c <- function(expr, data, ignore_case, regex, verbose, ifnotfound) { lst_expr <- as.list(expr) lst_expr[[1]] <- NULL unlist(lapply( @@ -366,40 +390,44 @@ data, ignore_case = ignore_case, regex = regex, - verbose = verbose + verbose = verbose, + ifnotfound = ifnotfound ), use.names = FALSE) } # e.g -(gear:cyl) -.select_bracket <- function(expr, data, ignore_case, regex, verbose) { +.select_bracket <- function(expr, data, ignore_case, regex, verbose, ifnotfound) { .eval_expr( expr[[2]], data, ignore_case = ignore_case, regex = regex, - verbose = verbose + verbose = verbose, + ifnotfound = ifnotfound ) } # e.g myvector[3] -.select_square_bracket <- function(expr, data, ignore_case, regex, verbose) { +.select_square_bracket <- function(expr, data, ignore_case, regex, verbose, ifnotfound) { first_obj <- .eval_expr( expr[[2]], data, ignore_case = ignore_case, regex = regex, - verbose = verbose + verbose = verbose, + ifnotfound = ifnotfound ) .eval_expr( first_obj[eval(expr[[3]])], data, ignore_case = ignore_case, regex = regex, - verbose = verbose + verbose = verbose, + ifnotfound = ifnotfound ) } -.select_names <- function(expr, data, ignore_case, regex, verbose) { +.select_names <- function(expr, data, ignore_case, regex, verbose, ifnotfound) { first_obj <- .dynEval(expr, inherits = FALSE, minframe = 0L) .eval_expr( first_obj, @@ -411,7 +439,7 @@ } # e.g starts_with("Sep") -.select_helper <- function(expr, data, ignore_case, regex, verbose) { +.select_helper <- function(expr, data, ignore_case, regex, verbose, ifnotfound) { lst_expr <- as.list(expr) # need this if condition to distinguish between starts_with("Sep") (that we @@ -435,7 +463,7 @@ } # e.g args$select (happens when we use grouped_data (see center.grouped_df())) -.select_dollar <- function(expr, data, ignore_case, regex, verbose) { +.select_dollar <- function(expr, data, ignore_case, regex, verbose, ifnotfound) { first_obj <- .dynGet(expr[[2]], ifnotfound = NULL, inherits = FALSE, minframe = 0L) if (is.null(first_obj)) { first_obj <- .dynEval(expr[[2]], inherits = FALSE, minframe = 0L) @@ -445,12 +473,13 @@ data, ignore_case = ignore_case, regex = regex, - verbose = verbose + verbose = verbose, + ifnotfound = ifnotfound ) } # e.g ~ gear + cyl -.select_tilde <- function(expr, data, ignore_case, regex, verbose) { +.select_tilde <- function(expr, data, ignore_case, regex, verbose, ifnotfound) { vars <- all.vars(expr) unlist(lapply( vars, @@ -458,12 +487,13 @@ data = data, ignore_case = ignore_case, regex = regex, - verbose = verbose + verbose = verbose, + ifnotfound = ifnotfound ), use.names = FALSE) } # e.g list(gear = 4, cyl = 5) -.select_list <- function(expr, data, ignore_case, regex, verbose) { +.select_list <- function(expr, data, ignore_case, regex, verbose, ifnotfound) { vars <- names(.dynEval(expr, inherits = FALSE, minframe = 0L)) unlist(lapply( vars, @@ -471,12 +501,13 @@ data = data, ignore_case = ignore_case, regex = regex, - verbose = verbose + verbose = verbose, + ifnotfound = ifnotfound ), use.names = FALSE) } # e.g is.numeric() -.select_context <- function(expr, data, ignore_case, regex, verbose) { +.select_context <- function(expr, data, ignore_case, regex, verbose, ifnotfound) { x_dep <- insight::safe_deparse(expr) if (endsWith(x_dep, "()")) { new_expr <- gsub("\\(\\)$", "", x_dep) @@ -486,7 +517,8 @@ data = data, ignore_case = ignore_case, regex = regex, - verbose = verbose + verbose = verbose, + ifnotfound = ifnotfound ) } else { out <- .dynEval(expr, inherits = FALSE, minframe = 0L) @@ -495,7 +527,8 @@ data = data, ignore_case = ignore_case, regex = regex, - verbose = verbose + verbose = verbose, + ifnotfound = ifnotfound ) } } diff --git a/R/text_format.R b/R/text_format.R index 0fa75bcac..46cbf2ee0 100644 --- a/R/text_format.R +++ b/R/text_format.R @@ -6,7 +6,7 @@ #' @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 Regex pattern to remove from `text`. #' @param sep Separator. #' @param last Last separator. #' @param n The number of characters to find. diff --git a/inst/WORDLIST b/inst/WORDLIST index eda7dc71c..a8b4ff08d 100644 --- a/inst/WORDLIST +++ b/inst/WORDLIST @@ -8,14 +8,14 @@ CMD Carle Catran Crosstables -Dhaliwal -Disaggregating +DEPRECATIONS DOI De -Dom +Dhaliwal +Disaggregating EFC -Enders EUROFAMCARE +Enders Fairbrother GLMM Gelman @@ -54,7 +54,6 @@ Winsorizing al behaviour behaviours -bmwiernik codebook codebooks coercible @@ -77,7 +76,6 @@ joss labelled labelling leptokurtic -lifecycle lm lme meaned @@ -88,7 +86,6 @@ modelling nd panelr partialization -patilindrajeets platykurtic poorman pre @@ -102,7 +99,6 @@ recodes recoding recodings relevel -rempsyc reproducibility rescale rescaled @@ -111,7 +107,6 @@ rio rowid sd stackexchange -strengejacke tailedness th tibble diff --git a/man/categorize.Rd b/man/categorize.Rd index 0fed929de..aaca6014c 100644 --- a/man/categorize.Rd +++ b/man/categorize.Rd @@ -244,8 +244,7 @@ categorize(mtcars$mpg, "equal_length", n_groups = 5, labels = "observed") } \seealso{ \itemize{ -\item Functions to rename stuff: \code{\link[=data_rename]{data_rename()}}, \code{\link[=data_rename_rows]{data_rename_rows()}}, -\code{\link[=data_addprefix]{data_addprefix()}}, \code{\link[=data_addsuffix]{data_addsuffix()}} +\item Add a prefix or suffix to column names: \code{\link[=data_addprefix]{data_addprefix()}}, \code{\link[=data_addsuffix]{data_addsuffix()}} \item Functions to reorder or remove columns: \code{\link[=data_reorder]{data_reorder()}}, \code{\link[=data_relocate]{data_relocate()}}, \code{\link[=data_remove]{data_remove()}} \item Functions to reshape, pivot or rotate data frames: \code{\link[=data_to_long]{data_to_long()}}, diff --git a/man/data_match.Rd b/man/data_match.Rd index 0354a44f4..84ad56664 100644 --- a/man/data_match.Rd +++ b/man/data_match.Rd @@ -128,8 +128,7 @@ data_filter(mtcars, fl) } \seealso{ \itemize{ -\item Functions to rename stuff: \code{\link[=data_rename]{data_rename()}}, \code{\link[=data_rename_rows]{data_rename_rows()}}, -\code{\link[=data_addprefix]{data_addprefix()}}, \code{\link[=data_addsuffix]{data_addsuffix()}} +\item Add a prefix or suffix to column names: \code{\link[=data_addprefix]{data_addprefix()}}, \code{\link[=data_addsuffix]{data_addsuffix()}} \item Functions to reorder or remove columns: \code{\link[=data_reorder]{data_reorder()}}, \code{\link[=data_relocate]{data_relocate()}}, \code{\link[=data_remove]{data_remove()}} \item Functions to reshape, pivot or rotate data frames: \code{\link[=data_to_long]{data_to_long()}}, diff --git a/man/data_merge.Rd b/man/data_merge.Rd index 169771b27..6876b06a1 100644 --- a/man/data_merge.Rd +++ b/man/data_merge.Rd @@ -190,8 +190,7 @@ data_merge(list(x, y, z), join = "bind", by = "id", id = "source") } \seealso{ \itemize{ -\item Functions to rename stuff: \code{\link[=data_rename]{data_rename()}}, \code{\link[=data_rename_rows]{data_rename_rows()}}, -\code{\link[=data_addprefix]{data_addprefix()}}, \code{\link[=data_addsuffix]{data_addsuffix()}} +\item Add a prefix or suffix to column names: \code{\link[=data_addprefix]{data_addprefix()}}, \code{\link[=data_addsuffix]{data_addsuffix()}} \item Functions to reorder or remove columns: \code{\link[=data_reorder]{data_reorder()}}, \code{\link[=data_relocate]{data_relocate()}}, \code{\link[=data_remove]{data_remove()}} \item Functions to reshape, pivot or rotate data frames: \code{\link[=data_to_long]{data_to_long()}}, diff --git a/man/data_partition.Rd b/man/data_partition.Rd index 73eb28286..23015e1b3 100644 --- a/man/data_partition.Rd +++ b/man/data_partition.Rd @@ -68,8 +68,7 @@ lapply(out, function(i) table(i$Species)) } \seealso{ \itemize{ -\item Functions to rename stuff: \code{\link[=data_rename]{data_rename()}}, \code{\link[=data_rename_rows]{data_rename_rows()}}, -\code{\link[=data_addprefix]{data_addprefix()}}, \code{\link[=data_addsuffix]{data_addsuffix()}} +\item Add a prefix or suffix to column names: \code{\link[=data_addprefix]{data_addprefix()}}, \code{\link[=data_addsuffix]{data_addsuffix()}} \item Functions to reorder or remove columns: \code{\link[=data_reorder]{data_reorder()}}, \code{\link[=data_relocate]{data_relocate()}}, \code{\link[=data_remove]{data_remove()}} \item Functions to reshape, pivot or rotate data frames: \code{\link[=data_to_long]{data_to_long()}}, diff --git a/man/data_prefix_suffix.Rd b/man/data_prefix_suffix.Rd new file mode 100644 index 000000000..eab848058 --- /dev/null +++ b/man/data_prefix_suffix.Rd @@ -0,0 +1,103 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/data_addprefix.R +\name{data_addprefix} +\alias{data_addprefix} +\alias{data_addsuffix} +\title{Add a prefix or suffix to column names} +\usage{ +data_addprefix( + data, + pattern, + select = NULL, + exclude = NULL, + ignore_case = FALSE, + regex = FALSE, + verbose = TRUE, + ... +) + +data_addsuffix( + data, + pattern, + select = NULL, + exclude = NULL, + ignore_case = FALSE, + regex = FALSE, + verbose = TRUE, + ... +) +} +\arguments{ +\item{data}{A data frame.} + +\item{pattern}{A character string, which will be added as prefix or suffix +to the column names.} + +\item{select}{Variables that will be included when performing the required +tasks. Can be either +\itemize{ +\item a variable specified as a literal variable name (e.g., \code{column_name}), +\item a string with the variable name (e.g., \code{"column_name"}), a character +vector of variable names (e.g., \code{c("col1", "col2", "col3")}), or a +character vector of variable names including ranges specified via \code{:} +(e.g., \code{c("col1:col3", "col5")}), +\item a formula with variable names (e.g., \code{~column_1 + column_2}), +\item a vector of positive integers, giving the positions counting from the left +(e.g. \code{1} or \code{c(1, 3, 5)}), +\item a vector of negative integers, giving the positions counting from the +right (e.g., \code{-1} or \code{-1:-3}), +\item one of the following select-helpers: \code{starts_with()}, \code{ends_with()}, +\code{contains()}, a range using \code{:}, or \code{regex()}. \code{starts_with()}, +\code{ends_with()}, and \code{contains()} accept several patterns, e.g +\code{starts_with("Sep", "Petal")}. \code{regex()} can be used to define regular +expression patterns. +\item a function testing for logical conditions, e.g. \code{is.numeric()} (or +\code{is.numeric}), or any user-defined function that selects the variables +for which the function returns \code{TRUE} (like: \code{foo <- function(x) mean(x) > 3}), +\item ranges specified via literal variable names, select-helpers (except +\code{regex()}) and (user-defined) functions can be negated, i.e. return +non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with()}, +\code{-is.numeric} or \code{-(Sepal.Width:Petal.Length)}. \strong{Note:} Negation means +that matches are \emph{excluded}, and thus, the \code{exclude} argument can be +used alternatively. For instance, \code{select=-ends_with("Length")} (with +\code{-}) is equivalent to \code{exclude=ends_with("Length")} (no \code{-}). In case +negation should not work as expected, use the \code{exclude} argument instead. +} + +If \code{NULL}, selects all columns. Patterns that found no matches are silently +ignored, e.g. \code{extract_column_names(iris, select = c("Species", "Test"))} +will just return \code{"Species"}.} + +\item{exclude}{See \code{select}, however, column names matched by the pattern +from \code{exclude} will be excluded instead of selected. If \code{NULL} (the default), +excludes no columns.} + +\item{ignore_case}{Logical, if \code{TRUE} and when one of the select-helpers or +a regular expression is used in \code{select}, ignores lower/upper case in the +search pattern when matching against variable names.} + +\item{regex}{Logical, if \code{TRUE}, the search pattern from \code{select} will be +treated as regular expression. When \code{regex = TRUE}, select \emph{must} be a +character string (or a variable containing a character string) and is not +allowed to be one of the supported select-helpers or a character vector +of length > 1. \code{regex = TRUE} is comparable to using one of the two +select-helpers, \code{select = contains()} or \code{select = regex()}, however, +since the select-helpers may not work when called from inside other +functions (see 'Details'), this argument may be used as workaround.} + +\item{verbose}{Toggle warnings.} + +\item{...}{Other arguments passed to or from other functions.} +} +\description{ +Add a prefix or suffix to column names +} +\examples{ +# Add prefix / suffix to all columns +head(data_addprefix(iris, "NEW_")) +head(data_addsuffix(iris, "_OLD")) + +} +\seealso{ +\code{\link[=data_rename]{data_rename()}} for more fine-grained column renaming. +} diff --git a/man/data_relocate.Rd b/man/data_relocate.Rd index 5991691cb..a1227dcfa 100644 --- a/man/data_relocate.Rd +++ b/man/data_relocate.Rd @@ -135,8 +135,7 @@ head(data_remove(iris, starts_with("Sepal"))) } \seealso{ \itemize{ -\item Functions to rename stuff: \code{\link[=data_rename]{data_rename()}}, \code{\link[=data_rename_rows]{data_rename_rows()}}, -\code{\link[=data_addprefix]{data_addprefix()}}, \code{\link[=data_addsuffix]{data_addsuffix()}} +\item Add a prefix or suffix to column names: \code{\link[=data_addprefix]{data_addprefix()}}, \code{\link[=data_addsuffix]{data_addsuffix()}} \item Functions to reorder or remove columns: \code{\link[=data_reorder]{data_reorder()}}, \code{\link[=data_relocate]{data_relocate()}}, \code{\link[=data_remove]{data_remove()}} \item Functions to reshape, pivot or rotate data frames: \code{\link[=data_to_long]{data_to_long()}}, diff --git a/man/data_rename.Rd b/man/data_rename.Rd index 3fea0fac4..8d99fb54f 100644 --- a/man/data_rename.Rd +++ b/man/data_rename.Rd @@ -1,40 +1,17 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/data_addprefix.R, R/data_rename.R -\name{data_addprefix} -\alias{data_addprefix} -\alias{data_addsuffix} +% Please edit documentation in R/data_rename.R +\name{data_rename} \alias{data_rename} \alias{data_rename_rows} \title{Rename columns and variable names} \usage{ -data_addprefix( - data, - pattern, - select = NULL, - exclude = NULL, - ignore_case = FALSE, - regex = FALSE, - verbose = TRUE, - ... -) - -data_addsuffix( - data, - pattern, - select = NULL, - exclude = NULL, - ignore_case = FALSE, - regex = FALSE, - verbose = TRUE, - ... -) - data_rename( data, - pattern = NULL, + select = NULL, replacement = NULL, safe = TRUE, verbose = TRUE, + pattern = NULL, ... ) @@ -43,18 +20,6 @@ data_rename_rows(data, rows = NULL) \arguments{ \item{data}{A data frame, or an object that can be coerced to a data frame.} -\item{pattern}{Character vector. -\itemize{ -\item For \code{data_addprefix()} or \code{data_addsuffix()}, a character string, which -will be added as prefix or suffix to the column names. -\item For \code{data_rename()}, indicates columns that should be selected for -renaming. Can be \code{NULL} (in which case all columns are selected). -\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{ = ""} and argument \code{replacement} will -be ignored then). -}} - \item{select}{Variables that will be included when performing the required tasks. Can be either \itemize{ @@ -90,41 +55,21 @@ If \code{NULL}, selects all columns. Patterns that found no matches are silently ignored, e.g. \code{extract_column_names(iris, select = c("Species", "Test"))} will just return \code{"Species"}.} -\item{exclude}{See \code{select}, however, column names matched by the pattern -from \code{exclude} will be excluded instead of selected. If \code{NULL} (the default), -excludes no columns.} - -\item{ignore_case}{Logical, if \code{TRUE} and when one of the select-helpers or -a regular expression is used in \code{select}, ignores lower/upper case in the -search pattern when matching against variable names.} - -\item{regex}{Logical, if \code{TRUE}, the search pattern from \code{select} will be -treated as regular expression. When \code{regex = TRUE}, select \emph{must} be a -character string (or a variable containing a character string) and is not -allowed to be one of the supported select-helpers or a character vector -of length > 1. \code{regex = TRUE} is comparable to using one of the two -select-helpers, \code{select = contains()} or \code{select = regex()}, however, -since the select-helpers may not work when called from inside other -functions (see 'Details'), this argument may be used as workaround.} - -\item{verbose}{Toggle warnings and messages.} - -\item{...}{Other arguments passed to or from other functions.} - \item{replacement}{Character vector. Can be one of the following: \itemize{ \item A character vector that indicates the new names of the columns selected -in \code{pattern}. \code{pattern} and \code{replacement} must be of the same length. -\item \code{NULL}, in which case columns are numbered in sequential order. -\item A string (i.e. character vector of length 1) with a "glue" styled pattern. -Currently supported tokens are: +in \code{select}. \code{select} and \code{replacement} must be of the same length. +\item A string (i.e. character vector of length 1) with a "glue" styled +pattern. Currently supported tokens are: \itemize{ \item \code{{col}} which will be replaced by the column name, i.e. the -corresponding value in \code{pattern}. +corresponding value in \code{select}. \item \code{{n}} will be replaced by the number of the variable that is replaced. -\item \code{{letter}} will be replaced by alphabetical letters in sequential order. +\item \code{{letter}} will be replaced by alphabetical letters in sequential +order. If more than 26 letters are required, letters are repeated, but have -sequential numeric indices (e.g., \code{a1} to \code{z1}, followed by \code{a2} to \code{z2}). +sequential numeric indices (e.g., \code{a1} to \code{z1}, followed by \code{a2} to +\code{z2}). \item Finally, the name of a user-defined object that is available in the environment can be used. Note that the object's name is not allowed to be one of the pre-defined tokens, \code{"col"}, \code{"n"} and \code{"letter"}. @@ -134,7 +79,7 @@ An example for the use of tokens is... \if{html}{\out{
}}\preformatted{data_rename( mtcars, - pattern = c("am", "vs"), + select = c("am", "vs"), replacement = "new_name_from_\{col\}" ) }\if{html}{\out{
}} @@ -143,10 +88,15 @@ An example for the use of tokens is... \code{new_name_from_vs}. See 'Examples'. } -If \code{pattern} is a named vector, \code{replacement} is ignored.} +If \code{select} is a named vector, \code{replacement} is ignored.} -\item{safe}{Do not throw error if for instance the variable to be -renamed/removed doesn't exist.} +\item{safe}{Deprecated. Passing unknown column names now always errors.} + +\item{verbose}{Toggle warnings.} + +\item{pattern}{Deprecated. Use \code{select} instead.} + +\item{...}{Other arguments passed to or from other functions.} \item{rows}{Vector of row names.} } @@ -156,29 +106,22 @@ A modified data frame. \description{ Safe and intuitive functions to rename variables or rows in data frames. \code{data_rename()} will rename column names, i.e. it facilitates -renaming variables \code{data_addprefix()} or \code{data_addsuffix()} add prefixes -or suffixes to column names. \code{data_rename_rows()} is a convenient shortcut +renaming variables. \code{data_rename_rows()} is a convenient shortcut to add or rename row names of a data frame, but unlike \code{row.names()}, its -input and output is a data frame, thus, integrating smoothly into a possible -pipe-workflow. +input and output is a data frame, thus, integrating smoothly into a +possible pipe-workflow. +} +\details{ +\code{select} can also be a named character vector. In this case, the names are +used to rename the columns in the output data frame. See 'Examples'. } \examples{ -# Add prefix / suffix to all columns -head(data_addprefix(iris, "NEW_")) -head(data_addsuffix(iris, "_OLD")) - # Rename columns head(data_rename(iris, "Sepal.Length", "length")) -# data_rename(iris, "FakeCol", "length", safe=FALSE) # This fails -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)) - # Change all head(data_rename(iris, replacement = paste0("Var", 1:5))) @@ -193,8 +136,7 @@ head(data_rename(mtcars[1:3], c("mpg", "cyl", "disp"), "col_{x}")) } \seealso{ \itemize{ -\item Functions to rename stuff: \code{\link[=data_rename]{data_rename()}}, \code{\link[=data_rename_rows]{data_rename_rows()}}, -\code{\link[=data_addprefix]{data_addprefix()}}, \code{\link[=data_addsuffix]{data_addsuffix()}} +\item Add a prefix or suffix to column names: \code{\link[=data_addprefix]{data_addprefix()}}, \code{\link[=data_addsuffix]{data_addsuffix()}} \item Functions to reorder or remove columns: \code{\link[=data_reorder]{data_reorder()}}, \code{\link[=data_relocate]{data_relocate()}}, \code{\link[=data_remove]{data_remove()}} \item Functions to reshape, pivot or rotate data frames: \code{\link[=data_to_long]{data_to_long()}}, diff --git a/man/data_rotate.Rd b/man/data_rotate.Rd index 25ba9a82b..6967a069a 100644 --- a/man/data_rotate.Rd +++ b/man/data_rotate.Rd @@ -52,8 +52,7 @@ data_rotate(x, colnames = "c") } \seealso{ \itemize{ -\item Functions to rename stuff: \code{\link[=data_rename]{data_rename()}}, \code{\link[=data_rename_rows]{data_rename_rows()}}, -\code{\link[=data_addprefix]{data_addprefix()}}, \code{\link[=data_addsuffix]{data_addsuffix()}} +\item Add a prefix or suffix to column names: \code{\link[=data_addprefix]{data_addprefix()}}, \code{\link[=data_addsuffix]{data_addsuffix()}} \item Functions to reorder or remove columns: \code{\link[=data_reorder]{data_reorder()}}, \code{\link[=data_relocate]{data_relocate()}}, \code{\link[=data_remove]{data_remove()}} \item Functions to reshape, pivot or rotate data frames: \code{\link[=data_to_long]{data_to_long()}}, diff --git a/man/data_to_long.Rd b/man/data_to_long.Rd index 3e587c049..36a474b83 100644 --- a/man/data_to_long.Rd +++ b/man/data_to_long.Rd @@ -223,8 +223,7 @@ head(even_longer_data) } \seealso{ \itemize{ -\item Functions to rename stuff: \code{\link[=data_rename]{data_rename()}}, \code{\link[=data_rename_rows]{data_rename_rows()}}, -\code{\link[=data_addprefix]{data_addprefix()}}, \code{\link[=data_addsuffix]{data_addsuffix()}} +\item Add a prefix or suffix to column names: \code{\link[=data_addprefix]{data_addprefix()}}, \code{\link[=data_addsuffix]{data_addsuffix()}} \item Functions to reorder or remove columns: \code{\link[=data_reorder]{data_reorder()}}, \code{\link[=data_relocate]{data_relocate()}}, \code{\link[=data_remove]{data_remove()}} \item Functions to reshape, pivot or rotate data frames: \code{\link[=data_to_long]{data_to_long()}}, diff --git a/man/data_to_wide.Rd b/man/data_to_wide.Rd index 3690eed53..62dc14f8d 100644 --- a/man/data_to_wide.Rd +++ b/man/data_to_wide.Rd @@ -208,8 +208,7 @@ data_to_wide( } \seealso{ \itemize{ -\item Functions to rename stuff: \code{\link[=data_rename]{data_rename()}}, \code{\link[=data_rename_rows]{data_rename_rows()}}, -\code{\link[=data_addprefix]{data_addprefix()}}, \code{\link[=data_addsuffix]{data_addsuffix()}} +\item Add a prefix or suffix to column names: \code{\link[=data_addprefix]{data_addprefix()}}, \code{\link[=data_addsuffix]{data_addsuffix()}} \item Functions to reorder or remove columns: \code{\link[=data_reorder]{data_reorder()}}, \code{\link[=data_relocate]{data_relocate()}}, \code{\link[=data_remove]{data_remove()}} \item Functions to reshape, pivot or rotate data frames: \code{\link[=data_to_long]{data_to_long()}}, diff --git a/man/extract_column_names.Rd b/man/extract_column_names.Rd index fe334e22f..d74d092c0 100644 --- a/man/extract_column_names.Rd +++ b/man/extract_column_names.Rd @@ -179,8 +179,7 @@ head(data_select(mtcars, c(`Miles per Gallon` = "mpg", Cylinders = "cyl"))) } \seealso{ \itemize{ -\item Functions to rename stuff: \code{\link[=data_rename]{data_rename()}}, \code{\link[=data_rename_rows]{data_rename_rows()}}, -\code{\link[=data_addprefix]{data_addprefix()}}, \code{\link[=data_addsuffix]{data_addsuffix()}} +\item Add a prefix or suffix to column names: \code{\link[=data_addprefix]{data_addprefix()}}, \code{\link[=data_addsuffix]{data_addsuffix()}} \item Functions to reorder or remove columns: \code{\link[=data_reorder]{data_reorder()}}, \code{\link[=data_relocate]{data_relocate()}}, \code{\link[=data_remove]{data_remove()}} \item Functions to reshape, pivot or rotate data frames: \code{\link[=data_to_long]{data_to_long()}}, diff --git a/man/recode_values.Rd b/man/recode_values.Rd index 11f9cfadc..d738093cb 100644 --- a/man/recode_values.Rd +++ b/man/recode_values.Rd @@ -279,8 +279,7 @@ options(data_recode_pattern = NULL) } \seealso{ \itemize{ -\item Functions to rename stuff: \code{\link[=data_rename]{data_rename()}}, \code{\link[=data_rename_rows]{data_rename_rows()}}, -\code{\link[=data_addprefix]{data_addprefix()}}, \code{\link[=data_addsuffix]{data_addsuffix()}} +\item Add a prefix or suffix to column names: \code{\link[=data_addprefix]{data_addprefix()}}, \code{\link[=data_addsuffix]{data_addsuffix()}} \item Functions to reorder or remove columns: \code{\link[=data_reorder]{data_reorder()}}, \code{\link[=data_relocate]{data_relocate()}}, \code{\link[=data_remove]{data_remove()}} \item Functions to reshape, pivot or rotate data frames: \code{\link[=data_to_long]{data_to_long()}}, diff --git a/man/rescale.Rd b/man/rescale.Rd index 8d6025a8d..f163a6e8c 100644 --- a/man/rescale.Rd +++ b/man/rescale.Rd @@ -132,6 +132,10 @@ A rescaled object. Rescale variables to a new range. Can also be used to reverse-score variables (change the keying/scoring direction), or to expand a range. } +\details{ +\code{select} can also be a named character vector. In this case, the names are +used to rename the columns in the output data frame. See 'Examples'. +} \section{Selection of variables - the \code{select} argument}{ For most functions that have a \code{select} argument (including this function), diff --git a/man/slide.Rd b/man/slide.Rd index d68f7e30a..c350ab7e4 100644 --- a/man/slide.Rd +++ b/man/slide.Rd @@ -124,8 +124,7 @@ sapply(mtcars, min) } \seealso{ \itemize{ -\item Functions to rename stuff: \code{\link[=data_rename]{data_rename()}}, \code{\link[=data_rename_rows]{data_rename_rows()}}, -\code{\link[=data_addprefix]{data_addprefix()}}, \code{\link[=data_addsuffix]{data_addsuffix()}} +\item Add a prefix or suffix to column names: \code{\link[=data_addprefix]{data_addprefix()}}, \code{\link[=data_addsuffix]{data_addsuffix()}} \item Functions to reorder or remove columns: \code{\link[=data_reorder]{data_reorder()}}, \code{\link[=data_relocate]{data_relocate()}}, \code{\link[=data_remove]{data_remove()}} \item Functions to reshape, pivot or rotate data frames: \code{\link[=data_to_long]{data_to_long()}}, diff --git a/man/text_format.Rd b/man/text_format.Rd index 16c76e67c..082e3cbe1 100644 --- a/man/text_format.Rd +++ b/man/text_format.Rd @@ -50,17 +50,7 @@ text elements will not be enclosed.} \item{n}{The number of characters to find.} -\item{pattern}{Character vector. -\itemize{ -\item For \code{data_addprefix()} or \code{data_addsuffix()}, a character string, which -will be added as prefix or suffix to the column names. -\item For \code{data_rename()}, indicates columns that should be selected for -renaming. Can be \code{NULL} (in which case all columns are selected). -\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{ = ""} and argument \code{replacement} will -be ignored then). -}} +\item{pattern}{Regex pattern to remove from \code{text}.} } \value{ A character string. diff --git a/man/winsorize.Rd b/man/winsorize.Rd index 15fa6af9b..fd9e38c64 100644 --- a/man/winsorize.Rd +++ b/man/winsorize.Rd @@ -82,8 +82,7 @@ winsorize(iris, threshold = 0.2) } \seealso{ \itemize{ -\item Functions to rename stuff: \code{\link[=data_rename]{data_rename()}}, \code{\link[=data_rename_rows]{data_rename_rows()}}, -\code{\link[=data_addprefix]{data_addprefix()}}, \code{\link[=data_addsuffix]{data_addsuffix()}} +\item Add a prefix or suffix to column names: \code{\link[=data_addprefix]{data_addprefix()}}, \code{\link[=data_addsuffix]{data_addsuffix()}} \item Functions to reorder or remove columns: \code{\link[=data_reorder]{data_reorder()}}, \code{\link[=data_relocate]{data_relocate()}}, \code{\link[=data_remove]{data_remove()}} \item Functions to reshape, pivot or rotate data frames: \code{\link[=data_to_long]{data_to_long()}}, diff --git a/pkgdown/_pkgdown.yaml b/pkgdown/_pkgdown.yaml index 31ec901d0..01b701f03 100644 --- a/pkgdown/_pkgdown.yaml +++ b/pkgdown/_pkgdown.yaml @@ -98,6 +98,7 @@ reference: Primarily useful in the context of other 'easystats' packages contents: - reshape_ci + - data_rename - data_addprefix - remove_empty - contains("rownames") diff --git a/tests/testthat/test-attributes.R b/tests/testthat/test-attributes.R index ebd26de99..df1ec0302 100644 --- a/tests/testthat/test-attributes.R +++ b/tests/testthat/test-attributes.R @@ -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") }) diff --git a/tests/testthat/test-data_rename.R b/tests/testthat/test-data_rename.R index 79f4427b3..3495a3355 100644 --- a/tests/testthat/test-data_rename.R +++ b/tests/testthat/test-data_rename.R @@ -20,31 +20,34 @@ test_that("data_rename works with one or several replacements", { ) }) +test_that("data_rename cannot have a partially named vector", { + expect_error( + data_rename(test, c(length = "Sepal.Length", "Sepal.Width")), + "all elements must" + ) +}) + test_that("data_rename returns a data frame", { x <- data_rename(test, "Sepal.Length", "length") expect_s3_class(x, "data.frame") }) -test_that("data_rename: pattern must be of type character", { - expect_error( - data_rename(test, pattern = 1), - regexp = "Argument `pattern` must be of type character" +test_that("data_rename: multiple selection types", { + expect_named( + data_rename(test, select = 1, "foo"), + c("foo", names(iris)[2:5]) ) - expect_error( - data_rename(test, pattern = TRUE), - regexp = "Argument `pattern` must be of type character" + expect_named( + data_rename(test, select = regex("tal"), c("foo1", "foo2")), + c("Sepal.Length", "Sepal.Width", "foo1", "foo2", "Species") ) }) test_that("data_rename: replacement not allowed to have NA or empty strings", { - expect_error( - data_rename(test, pattern = c(test = "Species", "Sepal.Length")), - regexp = "Either name all elements of `pattern`" - ) expect_error( data_rename( test, - pattern = c("Species", "Sepal.Length"), + select = c("Species", "Sepal.Length"), replacement = c("foo", NA_character_) ), regexp = "`replacement` is not allowed" @@ -53,78 +56,62 @@ test_that("data_rename: replacement not allowed to have NA or empty strings", { # replacement ------------- -test_that("data_rename uses indices when no replacement", { - x <- data_rename(test, pattern = c("Sepal.Length", "Petal.Length")) - expect_identical(dim(test), dim(x)) - expect_named(x, c("1", "Sepal.Width", "2", "Petal.Width", "Species")) +test_that("data_rename errors when no replacement", { + expect_error( + data_rename(test, select = c("Sepal.Length", "Petal.Length")), + "There are more names in `select` than in `replacement`" + ) }) -test_that("data_rename works when too many names in 'replacement'", { - expect_message( - { - x <- data_rename(test, replacement = paste0("foo", 1:6)) - }, - "There are more names in" +test_that("data_rename errors when too many names in 'replacement'", { + expect_error( + data_rename(test, replacement = paste0("foo", 1:6)), + "There are more names in `replacement` than in `select`" ) - expect_identical(dim(test), dim(x)) - expect_named(x, paste0("foo", 1:5)) }) test_that("data_rename works when not enough names in 'replacement'", { - expect_message( - { - x <- data_rename(test, replacement = paste0("foo", 1:2)) - }, - "There are more names in" + expect_error( + data_rename(test, replacement = paste0("foo", 1:2)), + "There are more names in `select` than in `replacement`" ) - expect_identical(dim(test), dim(x)) - expect_named(x, c("foo1", "foo2", "Petal.Length", "Petal.Width", "Species")) }) -# no pattern -------------- - -test_that("data_rename uses the whole dataset when pattern = NULL", { - x1 <- data_rename(test) - x2 <- data_rename(test, pattern = names(test)) - expect_identical(dim(test), dim(x1)) - expect_identical(x1, x2) +# no select -------------- - x3 <- data_rename(test, replacement = paste0("foo", 1:5)) - x4 <- data_rename(test, pattern = names(test), replacement = paste0("foo", 1:5)) - expect_identical(dim(test), dim(x3)) - expect_identical(x3, x4) +test_that("data_rename errors when select = NULL", { + expect_error( + data_rename(test), + "more names in `select`" + ) }) # other -------------- -test_that("data_rename: argument 'safe' works", { - expect_message( - data_rename(iris, "FakeCol", "length", safe = TRUE), - "Variable `FakeCol` is not in your data frame" +test_that("data_rename: argument 'safe' is deprecated", { + expect_error( + data_rename(iris, "FakeCol", "length", verbose = FALSE), + "were not found" ) expect_error( - data_rename(iris, "FakeCol", "length", safe = FALSE), - "Variable `FakeCol` is not in your data frame" + expect_warning( + data_rename(iris, "FakeCol", "length", safe = FALSE, verbose = FALSE), + "used" + ) ) }) test_that("data_rename deals correctly with duplicated replacement", { x <- data_rename(test, - pattern = names(test)[1:4], + select = names(test)[1:4], replacement = c("foo", "bar", "foo", "bar") ) expect_identical(dim(test), dim(x)) expect_named(x[1:4], c("foo", "bar", "foo.2", "bar.2")) }) -test_that("data_rename doesn't change colname if invalid pattern", { - x <- suppressMessages(data_rename(test, "FakeCol", "length")) - expect_named(x, names(test)) -}) - - # preserve attributes -------------------------- @@ -142,7 +129,7 @@ test_that("data_rename preserves attributes", { }) -# glue-styled pattern -------------------------- +# glue-styled select -------------------------- test_that("data_rename glue-style", { data(mtcars) @@ -226,3 +213,11 @@ withr::with_environment( ) }) ) + +test_that("Argument `pattern` is deprecated", { + expect_warning( + head(data_rename(iris, pattern = "Sepal.Length", "length")), + "Argument `pattern` is deprecated. Please use `select` instead.", + fixed = TRUE + ) +}) diff --git a/vignettes/tidyverse_translation.Rmd b/vignettes/tidyverse_translation.Rmd index ae4b339b3..a7e5225fc 100644 --- a/vignettes/tidyverse_translation.Rmd +++ b/vignettes/tidyverse_translation.Rmd @@ -515,7 +515,7 @@ a vector of new names for these columns that must be of the same length. # ---------- datawizard ----------- starwars |> data_rename( - pattern = c("sex", "hair_color"), + select = c("sex", "hair_color"), replacement = c("Sex", "Hair Color") ) ``` @@ -547,7 +547,7 @@ to_rename <- names(starwars) starwars |> data_rename( - pattern = to_rename, + select = to_rename, replacement = tools::toTitleCase(gsub("_", " ", to_rename, fixed = TRUE)) ) ``` From d44230bfb10c208ce994fc730282c9bd0d68e9be Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 2 Dec 2024 14:59:37 +0100 Subject: [PATCH 7/8] fix missing ifnotfound --- DESCRIPTION | 2 +- R/select_nse.R | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index a6869430a..dffcdfc3a 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: datawizard Title: Easy Data Wrangling and Statistical Transformations -Version: 0.13.0.16 +Version: 0.13.0.17 Authors@R: c( person("Indrajeet", "Patil", , "patilindrajeet.science@gmail.com", role = "aut", comment = c(ORCID = "0000-0003-1995-6531")), diff --git a/R/select_nse.R b/R/select_nse.R index 2d0255d0a..7b211beea 100644 --- a/R/select_nse.R +++ b/R/select_nse.R @@ -290,7 +290,8 @@ data = data, ignore_case = ignore_case, regex = regex, - verbose = verbose + verbose = verbose, + ifnotfound = ifnotfound ) } else if (length(new_expr) == 1L && is.function(new_expr)) { out <- which(vapply(data, new_expr, FUN.VALUE = logical(1L))) @@ -301,7 +302,8 @@ data = data, ignore_case = ignore_case, regex = regex, - verbose = verbose + verbose = verbose, + ifnotfound = ifnotfound ), use.names = FALSE) } } From 25f8ec4de760e327bd9fec094e1ff2a28f75a2e4 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 2 Dec 2024 19:02:46 +0100 Subject: [PATCH 8/8] fix --- R/select_nse.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/R/select_nse.R b/R/select_nse.R index 7b211beea..a085a4ce3 100644 --- a/R/select_nse.R +++ b/R/select_nse.R @@ -436,7 +436,8 @@ data, ignore_case = ignore_case, regex = regex, - verbose = FALSE + verbose = FALSE, + ifnotfound = ifnotfound ) }