From 80437a37c58142e2dbfe0a17a31b9dd4427e9747 Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 21 Nov 2024 22:19:54 +0100 Subject: [PATCH 01/12] Allow curl-styled pattern for `data_rename()` --- DESCRIPTION | 2 +- R/data_rename.R | 83 ++++++++++++++++++++++++++++++++++++++-------- man/data_rename.Rd | 49 ++++++++++++++++++++------- man/text_format.Rd | 19 ++++++----- 4 files changed, 119 insertions(+), 34 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/R/data_rename.R b/R/data_rename.R index 18f45657b..1d541882a 100644 --- a/R/data_rename.R +++ b/R/data_rename.R @@ -10,18 +10,34 @@ #' 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 name of the columns selected in +#' `pattern`. `pattern` and `replacement` must be of the same length. +#' - `NULL`, in which case column are numbered in sequential order. +#' - A string (i.e. character vector of length 1) with a "curl" styled pattern. +#' Currently supported tokens are `{col}` and `{n}`. `{col}` 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. For instance, +#' ```r +#' data_rename( +#' mtcars, +#' pattern = c("am", "vs"), +#' replacement = "new_name_from_{col}" +#' ) +#' ``` +#' would returns 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,6 +61,10 @@ #' #' # Change all #' head(data_rename(iris, replacement = paste0("Var", 1:5))) +#' +#' # Use curl-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}")) #' @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()] @@ -122,6 +142,11 @@ data_rename <- function(data, } } + # check if we have "curl" styled replacement-string + curl_style <- length(replacement) == 1 && + grepl("{", replacement, fixed = TRUE) && + length(pattern) > 1 + if (length(replacement) > length(pattern) && verbose) { insight::format_alert( paste0( @@ -129,7 +154,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 && !curl_style) { insight::format_alert( paste0( "There are more names in `pattern` than in `replacement`. The last ", @@ -138,6 +163,11 @@ data_rename <- function(data, ) } + # if we have curl-styled replacement-string, create replacement pattern now + if (curl_style) { + replacement <- .curl_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 +197,33 @@ data_rename <- function(data, } +.curl_replacement <- function(pattern, replacement) { + # this function replaces "curl" tokens into their related + # real names/values. Currently, following tokens are accepted: + # - {col}: replacement is the name of the column (inidcated in "pattern") + # - {n}: replacement is the number of the variable out of n, that should be renamed + out <- rep_len("", length(pattern)) + for (i in seq_along(out)) { + # prepare pattern + column_name <- pattern[i] + out[i] <- replacement + # replace first accepted token + out[i] <- gsub( + "(.*)(\\{col\\})(.*)", + replacement = paste0("\\1", column_name, "\\3"), + x = out[i] + ) + # replace second accepted token + out[i] <- gsub( + "(.*)(\\{n\\})(.*)", + replacement = paste0("\\1", i, "\\3"), + x = out[i] + ) + } + out +} + + # Row.names ---------------------------------------------------------------- #' @rdname data_rename diff --git a/man/data_rename.Rd b/man/data_rename.Rd index 2ff779c21..0ba4fa381 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,28 @@ 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 name of the columns selected in +\code{pattern}. \code{pattern} and \code{replacement} must be of the same length. +\item \code{NULL}, in which case column are numbered in sequential order. +\item A string (i.e. character vector of length 1) with a "curl" styled pattern. +Currently supported tokens are \code{{col}} and \code{{n}}. \code{{col}} will be replaced +by the column name, i.e. the corresponding value in \code{pattern}. \code{{n}} will +be replaced by the number of the variable that is replaced. For instance, + +\if{html}{\out{
}}\preformatted{data_rename( + mtcars, + pattern = c("am", "vs"), + replacement = "new_name_from_\{col\}" +) +}\if{html}{\out{
}} + +would returns 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,6 +169,10 @@ head(data_rename(iris, NULL)) # Change all head(data_rename(iris, replacement = paste0("Var", 1:5))) + +# Use curl-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}")) } \seealso{ \itemize{ 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. From c6b15007a1872fe0171e97dc59622d8812f0332b Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 21 Nov 2024 22:24:22 +0100 Subject: [PATCH 02/12] add test --- R/data_rename.R | 8 ++++---- man/data_rename.Rd | 8 ++++---- tests/testthat/test-data_rename.R | 11 +++++++++++ 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/R/data_rename.R b/R/data_rename.R index 1d541882a..8e8f83421 100644 --- a/R/data_rename.R +++ b/R/data_rename.R @@ -20,9 +20,9 @@ #' 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 name of the columns selected in -#' `pattern`. `pattern` and `replacement` must be of the same length. -#' - `NULL`, in which case column are numbered in sequential order. +#' - 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 "curl" styled pattern. #' Currently supported tokens are `{col}` and `{n}`. `{col}` will be replaced #' by the column name, i.e. the corresponding value in `pattern`. `{n}` will @@ -34,7 +34,7 @@ #' replacement = "new_name_from_{col}" #' ) #' ``` -#' would returns new column names `new_name_from_am` and `new_name_from__vs`. +#' would returns new column names `new_name_from_am` and `new_name_from_vs`. #' See 'Examples'. #' #' If `pattern` is a named vector, `replacement` is ignored. diff --git a/man/data_rename.Rd b/man/data_rename.Rd index 0ba4fa381..5dd11e0d9 100644 --- a/man/data_rename.Rd +++ b/man/data_rename.Rd @@ -112,9 +112,9 @@ functions (see 'Details'), this argument may be used as workaround.} \item{replacement}{Character vector. Can be one of the following: \itemize{ -\item A character vector that indicates the new name of the columns selected in -\code{pattern}. \code{pattern} and \code{replacement} must be of the same length. -\item \code{NULL}, in which case column are numbered in sequential order. +\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 "curl" styled pattern. Currently supported tokens are \code{{col}} and \code{{n}}. \code{{col}} will be replaced by the column name, i.e. the corresponding value in \code{pattern}. \code{{n}} will @@ -127,7 +127,7 @@ be replaced by the number of the variable that is replaced. For instance, ) }\if{html}{\out{}} -would returns new column names \code{new_name_from_am} and \code{new_name_from__vs}. +would returns new column names \code{new_name_from_am} and \code{new_name_from_vs}. See 'Examples'. } diff --git a/tests/testthat/test-data_rename.R b/tests/testthat/test-data_rename.R index e01c42f8b..2b6d744b3 100644 --- a/tests/testthat/test-data_rename.R +++ b/tests/testthat/test-data_rename.R @@ -140,3 +140,14 @@ test_that("data_rename preserves attributes", { expect_named(a1, names(a2)) }) + + +# curl-styled pattern -------------------------- + +test_that("data_rename curl-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")) +}) From 8e059e8deab4cd0e2d1c0a3b4a2c40469928a47d Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 21 Nov 2024 22:25:42 +0100 Subject: [PATCH 03/12] news --- NEWS.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/NEWS.md b/NEWS.md index 663efa310..54b02a8ab 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 curl-styled + tokens (#563). BUG FIXES From 5949e7ef5da4f04f236cd1527427fd1fcadf40e5 Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 21 Nov 2024 22:31:38 +0100 Subject: [PATCH 04/12] lintr --- R/data_rename.R | 12 ++++++++---- 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 | 12 ++++++++---- 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/winsorize.Rd | 12 ++++++++---- 14 files changed, 112 insertions(+), 56 deletions(-) diff --git a/R/data_rename.R b/R/data_rename.R index 8e8f83421..d5fb2ccd0 100644 --- a/R/data_rename.R +++ b/R/data_rename.R @@ -66,12 +66,16 @@ #' 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}")) #' @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()] 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 5dd11e0d9..54c79f7ef 100644 --- a/man/data_rename.Rd +++ b/man/data_rename.Rd @@ -176,12 +176,16 @@ head(data_rename(mtcars[1:3], c("mpg", "cyl", "disp"), "{col}_is_column_{n}")) } \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/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()}} From 03ff678788252222e621b55ef98559521e2b4837 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 22 Nov 2024 08:21:37 +0100 Subject: [PATCH 05/12] curl -> glue --- NEWS.md | 2 +- R/data_rename.R | 20 ++++++++++---------- man/data_rename.Rd | 4 ++-- tests/testthat/test-data_rename.R | 4 ++-- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/NEWS.md b/NEWS.md index 54b02a8ab..15b7f853d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -22,7 +22,7 @@ CHANGES * `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 curl-styled +* 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 d5fb2ccd0..9823c34ba 100644 --- a/R/data_rename.R +++ b/R/data_rename.R @@ -23,7 +23,7 @@ #' - 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 "curl" styled pattern. +#' - A string (i.e. character vector of length 1) with a "glue" styled pattern. #' Currently supported tokens are `{col}` and `{n}`. `{col}` 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. For instance, @@ -62,7 +62,7 @@ #' # Change all #' head(data_rename(iris, replacement = paste0("Var", 1:5))) #' -#' # Use curl-styled patterns +#' # 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}")) #' @seealso @@ -146,8 +146,8 @@ data_rename <- function(data, } } - # check if we have "curl" styled replacement-string - curl_style <- length(replacement) == 1 && + # check if we have "glue" styled replacement-string + glue_style <- length(replacement) == 1 && grepl("{", replacement, fixed = TRUE) && length(pattern) > 1 @@ -158,7 +158,7 @@ data_rename <- function(data, length(replacement) - length(pattern), " names of `replacement` are not used." ) ) - } else if (length(replacement) < length(pattern) && verbose && !curl_style) { + } else if (length(replacement) < length(pattern) && verbose && !glue_style) { insight::format_alert( paste0( "There are more names in `pattern` than in `replacement`. The last ", @@ -167,9 +167,9 @@ data_rename <- function(data, ) } - # if we have curl-styled replacement-string, create replacement pattern now - if (curl_style) { - replacement <- .curl_replacement(pattern, replacement) + # 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)) { @@ -201,8 +201,8 @@ data_rename <- function(data, } -.curl_replacement <- function(pattern, replacement) { - # this function replaces "curl" tokens into their related +.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 (inidcated in "pattern") # - {n}: replacement is the number of the variable out of n, that should be renamed diff --git a/man/data_rename.Rd b/man/data_rename.Rd index 54c79f7ef..96bdf6501 100644 --- a/man/data_rename.Rd +++ b/man/data_rename.Rd @@ -115,7 +115,7 @@ functions (see 'Details'), this argument may be used as workaround.} \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 "curl" styled pattern. +\item A string (i.e. character vector of length 1) with a "glue" styled pattern. Currently supported tokens are \code{{col}} and \code{{n}}. \code{{col}} will be replaced by the column name, i.e. the corresponding value in \code{pattern}. \code{{n}} will be replaced by the number of the variable that is replaced. For instance, @@ -170,7 +170,7 @@ head(data_rename(iris, NULL)) # Change all head(data_rename(iris, replacement = paste0("Var", 1:5))) -# Use curl-styled patterns +# 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}")) } diff --git a/tests/testthat/test-data_rename.R b/tests/testthat/test-data_rename.R index 2b6d744b3..c5bb286e4 100644 --- a/tests/testthat/test-data_rename.R +++ b/tests/testthat/test-data_rename.R @@ -142,9 +142,9 @@ test_that("data_rename preserves attributes", { }) -# curl-styled pattern -------------------------- +# glue-styled pattern -------------------------- -test_that("data_rename curl-style", { +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")) From 4c344331b60143493ab759e5b5e9727a2840c1e3 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 24 Nov 2024 13:26:43 +0100 Subject: [PATCH 06/12] allow alias --- R/data_rename.R | 15 +++++++++++---- man/data_rename.Rd | 7 ++++--- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/R/data_rename.R b/R/data_rename.R index 9823c34ba..35bf73aab 100644 --- a/R/data_rename.R +++ b/R/data_rename.R @@ -24,9 +24,10 @@ #' 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}` and `{n}`. `{col}` 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. For instance, +#' Currently supported tokens are `{col}` (or `{name}`) and `{n}`. `{col}` +#' 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. For instance, #' ```r #' data_rename( #' mtcars, @@ -204,7 +205,7 @@ 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 (inidcated in "pattern") + # - {col}/{name}: replacement is the name of the column (indicated in "pattern") # - {n}: replacement is the number of the variable out of n, that should be renamed out <- rep_len("", length(pattern)) for (i in seq_along(out)) { @@ -217,6 +218,12 @@ data_rename <- function(data, replacement = paste0("\\1", column_name, "\\3"), x = out[i] ) + # alias of {col} is {name} + out[i] <- gsub( + "(.*)(\\{name\\})(.*)", + replacement = paste0("\\1", column_name, "\\3"), + x = out[i] + ) # replace second accepted token out[i] <- gsub( "(.*)(\\{n\\})(.*)", diff --git a/man/data_rename.Rd b/man/data_rename.Rd index 96bdf6501..4efb3a143 100644 --- a/man/data_rename.Rd +++ b/man/data_rename.Rd @@ -116,9 +116,10 @@ functions (see 'Details'), this argument may be used as workaround.} 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 \code{{col}} and \code{{n}}. \code{{col}} will be replaced -by the column name, i.e. the corresponding value in \code{pattern}. \code{{n}} will -be replaced by the number of the variable that is replaced. For instance, +Currently supported tokens are \code{{col}} (or \code{{name}}) and \code{{n}}. \code{{col}} +will be replaced by the column name, i.e. the corresponding value in +\code{pattern}. \code{{n}} will be replaced by the number of the variable that is +replaced. For instance, \if{html}{\out{
}}\preformatted{data_rename( mtcars, From 01a47364150d5c24f5dd428e281982d7aff2784e Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 24 Nov 2024 15:00:39 +0100 Subject: [PATCH 07/12] add options --- R/data_rename.R | 58 ++++++++++++++++++++++++++----- man/data_rename.Rd | 24 +++++++++---- tests/testthat/test-data_rename.R | 17 +++++++++ 3 files changed, 85 insertions(+), 14 deletions(-) diff --git a/R/data_rename.R b/R/data_rename.R index 35bf73aab..209d4960f 100644 --- a/R/data_rename.R +++ b/R/data_rename.R @@ -24,10 +24,15 @@ #' 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}` (or `{name}`) and `{n}`. `{col}` -#' 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. For instance, +#' Currently supported tokens are: +#' - `{col}` (or `{name}`), 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. +#' - Finally, the name of a user-defined object that is available in the +#' environment can be used. In this case, +#' +#' An example for the use of tokens is... #' ```r #' data_rename( #' mtcars, @@ -35,8 +40,8 @@ #' replacement = "new_name_from_{col}" #' ) #' ``` -#' would returns new column names `new_name_from_am` and `new_name_from_vs`. -#' See 'Examples'. +#' ... 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. @@ -66,6 +71,11 @@ #' # 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()] @@ -212,7 +222,7 @@ data_rename <- function(data, # prepare pattern column_name <- pattern[i] out[i] <- replacement - # replace first accepted token + # replace first pre-defined token out[i] <- gsub( "(.*)(\\{col\\})(.*)", replacement = paste0("\\1", column_name, "\\3"), @@ -224,12 +234,44 @@ data_rename <- function(data, replacement = paste0("\\1", column_name, "\\3"), x = out[i] ) - # replace second accepted token + # 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", 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))) + # 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 the columns to rename (", + length(pattern), " columns)." + )) + } + # replace token with values from the object + if (!is.null(values) && length(values)) { + out[i] <- gsub(token, values[i], out[i], fixed = TRUE) + } + } + } } out } diff --git a/man/data_rename.Rd b/man/data_rename.Rd index 4efb3a143..a975cac2e 100644 --- a/man/data_rename.Rd +++ b/man/data_rename.Rd @@ -116,10 +116,17 @@ functions (see 'Details'), this argument may be used as workaround.} 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 \code{{col}} (or \code{{name}}) and \code{{n}}. \code{{col}} -will be replaced by the column name, i.e. the corresponding value in -\code{pattern}. \code{{n}} will be replaced by the number of the variable that is -replaced. For instance, +Currently supported tokens are: +\itemize{ +\item \code{{col}} (or \code{{name}}), 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. +\item Finally, the name of a user-defined object that is available in the +environment can be used. In this case, +} + +An example for the use of tokens is... \if{html}{\out{
}}\preformatted{data_rename( mtcars, @@ -128,8 +135,8 @@ replaced. For instance, ) }\if{html}{\out{
}} -would returns new column names \code{new_name_from_am} and \code{new_name_from_vs}. -See 'Examples'. +... 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.} @@ -174,6 +181,11 @@ 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{ diff --git a/tests/testthat/test-data_rename.R b/tests/testthat/test-data_rename.R index c5bb286e4..1a08cbd18 100644 --- a/tests/testthat/test-data_rename.R +++ b/tests/testthat/test-data_rename.R @@ -150,4 +150,21 @@ test_that("data_rename glue-style", { 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")) }) + +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" + ) + }) +) From fc18483531614997c9645e9b3de15a4b506df8b4 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 24 Nov 2024 15:08:29 +0100 Subject: [PATCH 08/12] typo --- R/data_rename.R | 4 ++-- man/data_rename.Rd | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/R/data_rename.R b/R/data_rename.R index 209d4960f..7cc9736f1 100644 --- a/R/data_rename.R +++ b/R/data_rename.R @@ -30,7 +30,7 @@ #' - `{n}` will be replaced by the number of the variable that is replaced. #' - `{letter}` will be replaced by alphabetical letters in sequential order. #' - Finally, the name of a user-defined object that is available in the -#' environment can be used. In this case, +#' environment can be used. #' #' An example for the use of tokens is... #' ```r @@ -262,7 +262,7 @@ data_rename <- function(data, 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 the columns to rename (", + " values) do not match the number of columns to rename (", length(pattern), " columns)." )) } diff --git a/man/data_rename.Rd b/man/data_rename.Rd index a975cac2e..3559b911f 100644 --- a/man/data_rename.Rd +++ b/man/data_rename.Rd @@ -123,7 +123,7 @@ 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. \item Finally, the name of a user-defined object that is available in the -environment can be used. In this case, +environment can be used. } An example for the use of tokens is... From 349c7c5b6ec9db5de9693f7a0926a02ca71de725 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 27 Nov 2024 15:19:10 +0100 Subject: [PATCH 09/12] address comments --- R/data_rename.R | 48 ++++++++++++++++--------- man/data_rename.Rd | 9 +++-- tests/testthat/test-data_rename.R | 58 +++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 19 deletions(-) diff --git a/R/data_rename.R b/R/data_rename.R index 7cc9736f1..a0929401a 100644 --- a/R/data_rename.R +++ b/R/data_rename.R @@ -25,12 +25,15 @@ #' - `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}` (or `{name}`), which will be replaced by the column name, i.e. -#' the corresponding value in `pattern`. +#' - `{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 +#' seqential 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. +#' 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 @@ -158,9 +161,7 @@ data_rename <- function(data, } # check if we have "glue" styled replacement-string - glue_style <- length(replacement) == 1 && - grepl("{", replacement, fixed = TRUE) && - length(pattern) > 1 + glue_style <- length(replacement) == 1 && grepl("{", replacement, fixed = TRUE) if (length(replacement) > length(pattern) && verbose) { insight::format_alert( @@ -215,9 +216,23 @@ 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}/{name}: replacement is the name of the column (indicated in "pattern") + # - {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] @@ -228,12 +243,6 @@ data_rename <- function(data, replacement = paste0("\\1", column_name, "\\3"), x = out[i] ) - # alias of {col} is {name} - out[i] <- gsub( - "(.*)(\\{name\\})(.*)", - replacement = paste0("\\1", column_name, "\\3"), - x = out[i] - ) # replace second pre-defined token out[i] <- gsub( "(.*)(\\{n\\})(.*)", @@ -243,7 +252,7 @@ data_rename <- function(data, # replace third pre-defined token out[i] <- gsub( "(.*)(\\{letter\\})(.*)", - replacement = paste0("\\1", letters[i], "\\3"), + replacement = paste0("\\1", long_letters[i], "\\3"), x = out[i] ) # extract all non-standard tokens @@ -257,7 +266,14 @@ data_rename <- function(data, # if so, iterate all tokens for (token in matches) { # evaluate token-object from the environment - values <- .dynEval(str2lang(gsub("\\{(.*)\\}", "\\1", token))) + values <- tryCatch( + .dynEval(str2lang(gsub("\\{(.*)\\}", "\\1", token))), + error = function(e) { + 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( @@ -267,7 +283,7 @@ data_rename <- function(data, )) } # replace token with values from the object - if (!is.null(values) && length(values)) { + if (length(values)) { out[i] <- gsub(token, values[i], out[i], fixed = TRUE) } } diff --git a/man/data_rename.Rd b/man/data_rename.Rd index 3559b911f..9edb603da 100644 --- a/man/data_rename.Rd +++ b/man/data_rename.Rd @@ -118,12 +118,15 @@ in \code{pattern}. \code{pattern} and \code{replacement} must be of the same len \item A string (i.e. character vector of length 1) with a "glue" styled pattern. Currently supported tokens are: \itemize{ -\item \code{{col}} (or \code{{name}}), which will be replaced by the column name, i.e. -the corresponding value in \code{pattern}. +\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 +seqential 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. +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... diff --git a/tests/testthat/test-data_rename.R b/tests/testthat/test-data_rename.R index 1a08cbd18..50d98c0d0 100644 --- a/tests/testthat/test-data_rename.R +++ b/tests/testthat/test-data_rename.R @@ -154,6 +154,35 @@ test_that("data_rename glue-style", { 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_names( + 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(), @@ -168,3 +197,32 @@ withr::with_environment( ) }) ) + +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 d6a5456bd92f9f9ed038fb25e4ccbcb6ca06c88e Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 27 Nov 2024 15:20:34 +0100 Subject: [PATCH 10/12] remove # in comment --- R/data_rename.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/data_rename.R b/R/data_rename.R index a0929401a..4810e43c7 100644 --- a/R/data_rename.R +++ b/R/data_rename.R @@ -222,7 +222,7 @@ data_rename <- function(data, out <- rep_len("", length(pattern)) # for alphabetical letters, we prepare a string if we have more than - # 26 columns # to rename + # 26 columns to rename if (length(out) > 26) { long_letters <- paste0( rep.int(letters[1:26], times = ceiling(length(out) / 26)), From 3d64e69e5e21046b6466e13042cf86c72a5757a6 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 27 Nov 2024 15:41:28 +0100 Subject: [PATCH 11/12] fix --- R/data_rename.R | 12 +++++------- tests/testthat/test-data_rename.R | 2 +- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/R/data_rename.R b/R/data_rename.R index 4810e43c7..c69f8d9bc 100644 --- a/R/data_rename.R +++ b/R/data_rename.R @@ -266,13 +266,11 @@ data_rename <- function(data, # if so, iterate all tokens for (token in matches) { # evaluate token-object from the environment - values <- tryCatch( - .dynEval(str2lang(gsub("\\{(.*)\\}", "\\1", token))), - error = function(e) { - insight::format_error(paste0( - "The object `", token, "` was not found. Please check if it really exists." - )) - } + 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)) { diff --git a/tests/testthat/test-data_rename.R b/tests/testthat/test-data_rename.R index 50d98c0d0..79f4427b3 100644 --- a/tests/testthat/test-data_rename.R +++ b/tests/testthat/test-data_rename.R @@ -166,7 +166,7 @@ test_that("data_rename enough letters", { mtcars[1:5, ], iris[1:5, ], efc[1:5, ], ChickWeight[1:5, ], ToothGrowth[1:5, ], USArrests[1:5, ], airquality[1:5, ] ) - expect_names( + expect_named( data_rename(x, replacement = "long_letter_{letter}"), c( "long_letter_a1", "long_letter_b1", "long_letter_c1", "long_letter_d1", From 5b4f385274f41037b93188a11be628a6ff72a91d Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 27 Nov 2024 15:55:06 +0100 Subject: [PATCH 12/12] typo --- R/data_rename.R | 4 ++-- man/data_rename.Rd | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/R/data_rename.R b/R/data_rename.R index c69f8d9bc..f5d6e0e03 100644 --- a/R/data_rename.R +++ b/R/data_rename.R @@ -30,7 +30,7 @@ #' - `{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 -#' seqential 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"`. @@ -266,7 +266,7 @@ data_rename <- function(data, # if so, iterate all tokens for (token in matches) { # evaluate token-object from the environment - values <- .dynEval( + values <- .dynEval( str2lang(gsub("\\{(.*)\\}", "\\1", token)), ifnotfound = insight::format_error(paste0( "The object `", token, "` was not found. Please check if it really exists." diff --git a/man/data_rename.Rd b/man/data_rename.Rd index 9edb603da..2859c04d7 100644 --- a/man/data_rename.Rd +++ b/man/data_rename.Rd @@ -123,7 +123,7 @@ 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 -seqential 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"}.