From c97eed4196703f6d42f6f97b4a5261cc278b6488 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 25 Mar 2022 18:49:25 +0100 Subject: [PATCH] Preserve attr (#142) * Update data_findcols.R * Update test-get_columns.R * Update test-data_relocate.R * add tests * Update test-data_remove.R * Update data_relocate.R * Update test-data_rename.R * Update test-convert_na_to.R * fix weighted_median * add format-method * docs, add alias * docs * typo --- NAMESPACE | 1 + R/data_findcols.R | 39 ++------------------------------------- R/data_relocate.R | 16 ++++++++-------- R/data_rename.R | 8 +++++++- R/data_select.R | 39 +++++++++++++++++++++++++++++++++++++++ man/data_relocate.Rd | 12 +++++------- man/data_rename.Rd | 8 +++++++- man/find_columns.Rd | 26 +++++++++++++++++++------- 8 files changed, 88 insertions(+), 61 deletions(-) create mode 100644 R/data_select.R diff --git a/NAMESPACE b/NAMESPACE index 8f8a07ff7..48b11e56f 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -133,6 +133,7 @@ export(data_rescale) export(data_restoretype) export(data_reverse) export(data_rotate) +export(data_select) export(data_to_long) export(data_to_numeric) export(data_to_wide) diff --git a/R/data_findcols.R b/R/data_findcols.R index 0368bbec8..355c5afe4 100644 --- a/R/data_findcols.R +++ b/R/data_findcols.R @@ -1,8 +1,9 @@ #' @title Find or get columns in a data frame based on search patterns #' @name find_columns #' -#' @description `find_columns()` returns colum names from a data set that +#' @description `find_columns()` returns column names from a data set that #' match a certain search pattern, while `get_columns()` returns the found data. +#' `data_select()` is an alias for `get_columns()`. #' #' @param data A data frame. #' @param select Variables that will be included when performing the required @@ -114,42 +115,6 @@ find_columns <- function(data, } -#' @rdname find_columns -#' @export -get_columns <- function(data, - select = NULL, - exclude = NULL, - ignore_case = FALSE, - regex = FALSE, - verbose = TRUE, - ...) { - columns <- .select_nse( - select, - data, - exclude, - ignore_case = ignore_case, - regex = regex, - verbose = FALSE - ) - - # save attributes - a <- attributes(data) - - if (!length(columns) || is.null(columns)) { - if (isTRUE(verbose)) { - warning(insight::format_message("No column names that matched the required search pattern were found."), call. = FALSE) - } - return(NULL) - } - - out <- data[columns] - - # add back attributes - attributes(out) <- utils::modifyList(a, attributes(out)) - out -} - - #' @param pattern A regular expression (as character string), representing the #' pattern to be matched in the in column names. Can also be one of the #' following select-helpers: `starts_with("")`, `end_with("")`, `regex("")`, diff --git a/R/data_relocate.R b/R/data_relocate.R index 3d97114fc..5e1296f05 100644 --- a/R/data_relocate.R +++ b/R/data_relocate.R @@ -1,6 +1,14 @@ #' @title Relocate (reorder) columns of a data frame #' @name data_relocate #' +#' @description +#' `data_relocate()` will reorder columns to specific positions, indicated by +#' `before` or `after`. `data_reorder()` will instead move selected columns to +#' the beginning of a data frame. Finally, `data_remove()` removes columns +#' from a data frame. All functions support select-helpers that allow flexible +#' specification of a search pattern to find matching columns, which should +#' be reordered or removed. +#' #' @param data A data frame. #' @param before,after Destination of columns. Supplying neither will move #' columns to the left-hand side; specifying both is an error. Can be a @@ -12,14 +20,6 @@ #' #' @inherit data_rename seealso #' -#' @details -#' -#' `data_relocate()` will reorder columns to specific positions, indicated by -#' `before` or `after`. -#' -#' `data_reorder()` will instead move selected columns to the beginning of the -#' data frame . -#' #' @return A data frame with reordered columns. #' #' @examples diff --git a/R/data_rename.R b/R/data_rename.R index a5ff91651..5057ef3bc 100644 --- a/R/data_rename.R +++ b/R/data_rename.R @@ -1,7 +1,13 @@ #' @title Rename columns and variable names #' @name data_rename #' -#' @description Safe and intuitive functions to rename variables or rows in dataframes. +#' @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 +#' 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. #' #' @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 diff --git a/R/data_select.R b/R/data_select.R new file mode 100644 index 000000000..844d78d0b --- /dev/null +++ b/R/data_select.R @@ -0,0 +1,39 @@ +#' @rdname find_columns +#' @export +get_columns <- function(data, + select = NULL, + exclude = NULL, + ignore_case = FALSE, + regex = FALSE, + verbose = TRUE, + ...) { + columns <- .select_nse( + select, + data, + exclude, + ignore_case = ignore_case, + regex = regex, + verbose = FALSE + ) + + # save attributes + a <- attributes(data) + + if (!length(columns) || is.null(columns)) { + if (isTRUE(verbose)) { + warning(insight::format_message("No column names that matched the required search pattern were found."), call. = FALSE) + } + return(NULL) + } + + out <- data[columns] + + # add back attributes + attributes(out) <- utils::modifyList(a, attributes(out)) + out +} + + +#' @rdname find_columns +#' @export +data_select <- get_columns diff --git a/man/data_relocate.Rd b/man/data_relocate.Rd index 13a143717..331db9132 100644 --- a/man/data_relocate.Rd +++ b/man/data_relocate.Rd @@ -58,14 +58,12 @@ search pattern when matching against variable names.} A data frame with reordered columns. } \description{ -Relocate (reorder) columns of a data frame -} -\details{ \code{data_relocate()} will reorder columns to specific positions, indicated by -\code{before} or \code{after}. - -\code{data_reorder()} will instead move selected columns to the beginning of the -data frame . +\code{before} or \code{after}. \code{data_reorder()} will instead move selected columns to +the beginning of a data frame. Finally, \code{data_remove()} removes columns +from a data frame. All functions support select-helpers that allow flexible +specification of a search pattern to find matching columns, which should +be reordered or removed. } \examples{ # Reorder columns diff --git a/man/data_rename.Rd b/man/data_rename.Rd index 347756a17..c3ea160ed 100644 --- a/man/data_rename.Rd +++ b/man/data_rename.Rd @@ -78,7 +78,13 @@ renamed/removed doesn't exist.} A modified data frame. } \description{ -Safe and intuitive functions to rename variables or rows in dataframes. +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 +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. } \examples{ # Add prefix / suffix to all columns diff --git a/man/find_columns.Rd b/man/find_columns.Rd index fb85249b6..219b79877 100644 --- a/man/find_columns.Rd +++ b/man/find_columns.Rd @@ -1,9 +1,10 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/data_findcols.R +% Please edit documentation in R/data_findcols.R, R/data_select.R \name{find_columns} \alias{find_columns} -\alias{get_columns} \alias{data_findcols} +\alias{get_columns} +\alias{data_select} \title{Find or get columns in a data frame based on search patterns} \usage{ find_columns( @@ -16,6 +17,15 @@ find_columns( ... ) +data_findcols( + data, + pattern = NULL, + starts_with = NULL, + ends_with = NULL, + ignore_case = FALSE, + ... +) + get_columns( data, select = NULL, @@ -26,12 +36,13 @@ get_columns( ... ) -data_findcols( +data_select( data, - pattern = NULL, - starts_with = NULL, - ends_with = NULL, + select = NULL, + exclude = NULL, ignore_case = FALSE, + regex = FALSE, + verbose = TRUE, ... ) } @@ -92,8 +103,9 @@ column name was found. \code{get_columns()} returns a data frame with matching columns. } \description{ -\code{find_columns()} returns colum names from a data set that +\code{find_columns()} returns column names from a data set that match a certain search pattern, while \code{get_columns()} returns the found data. +\code{data_select()} is an alias for \code{get_columns()}. } \details{ Note that there are some limitations when calling this from inside