From 60562c8b63f429b3c84d680fa2f4746499126b35 Mon Sep 17 00:00:00 2001 From: Will Beasley Date: Sat, 28 Oct 2023 12:40:10 -0500 Subject: [PATCH] examples ref #146 --- DESCRIPTION | 1 + R/row.R | 39 ++++++++++++++++++++++++++++++++++----- man/row_sum.Rd | 32 +++++++++++++++++++++++++++++++- 3 files changed, 66 insertions(+), 6 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 6a7aa71..be1026a 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -31,6 +31,7 @@ Suggests: RODBC, spelling, testthat, + tidyr, tinytex Encoding: UTF-8 Language: en-US diff --git a/R/row.R b/R/row.R index bde6bf7..ec7d04b 100644 --- a/R/row.R +++ b/R/row.R @@ -28,28 +28,57 @@ #' the new column will be an [integer]. #' Otherwise the new column will be a [double]. #' -#' @note #' @author Will Beasley #' @importFrom rlang := #' @examples #' library(OuhscMunge) #Load the package into the current R session. - +#' mtcars |> +#' row_sum( +#' columns_to_average = c("cyl", "disp", "vs", "carb"), +#' new_column_name = "engine_sum" +#' ) +#' +#' if (require(tidyr)) +#' tidyr::billboard |> +#' row_sum( +#' pattern = "^wk\\d{1,2}$", +#' new_column_name = "week_sum", +#' threshold_proportion = .1, +#' verbose = TRUE +#' ) |> +#' dplyr::select( +#' artist, +#' date.entered, +#' week_sum, +#' ) #' +#' tidyr::billboard |> +#' row_sum( +#' pattern = "^wk\\d$", +#' new_column_name = "week_sum", +#' verbose = TRUE +#' ) |> +#' dplyr::select( +#' artist, +#' date.entered, +#' week_sum, +#' ) + #' @export row_sum <- function( d, columns_to_average = character(0), - pattern, + pattern = "", new_column_name = "row_sum", threshold_proportion = .75, verbose = FALSE ) { checkmate::assert_data_frame(d) checkmate::assert_character(columns_to_average , any.missing = FALSE) - checkmate::assert_character(pattern , min.len = 0, max.len = 1) + checkmate::assert_character(pattern , len = 1) checkmate::assert_character(new_column_name , len = 1) checkmate::assert_double( threshold_proportion, len = 1) - checkmate::assert_logical( verbose , len = 1) + checkmate::assert_logical( verbose , len = 1) if (length(columns_to_average) == 0L) { columns_to_average <- diff --git a/man/row_sum.Rd b/man/row_sum.Rd index e0528ed..0358176 100644 --- a/man/row_sum.Rd +++ b/man/row_sum.Rd @@ -7,7 +7,7 @@ row_sum( d, columns_to_average = character(0), - pattern, + pattern = "", new_column_name = "row_sum", threshold_proportion = 0.75, verbose = FALSE @@ -50,7 +50,37 @@ Otherwise the new column will be a \link{double}. } \examples{ library(OuhscMunge) #Load the package into the current R session. +mtcars |> + row_sum( + columns_to_average = c("cyl", "disp", "vs", "carb"), + new_column_name = "engine_sum" + ) +if (require(tidyr)) + tidyr::billboard |> + row_sum( + pattern = "^wk\\\\d{1,2}$", + new_column_name = "week_sum", + threshold_proportion = .1, + verbose = TRUE + ) |> + dplyr::select( + artist, + date.entered, + week_sum, + ) + + tidyr::billboard |> + row_sum( + pattern = "^wk\\\\d$", + new_column_name = "week_sum", + verbose = TRUE + ) |> + dplyr::select( + artist, + date.entered, + week_sum, + ) } \author{ Will Beasley