diff --git a/DESCRIPTION b/DESCRIPTION index a3b81a578..a51982765 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: datawizard Title: Easy Data Wrangling and Statistical Transformations -Version: 0.9.1.7 +Version: 0.9.1.8 Authors@R: c( person("Indrajeet", "Patil", , "patilindrajeet.science@gmail.com", role = "aut", comment = c(ORCID = "0000-0003-1995-6531", Twitter = "@patilindrajeets")), diff --git a/NAMESPACE b/NAMESPACE index d10d1884b..eea4e22a4 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -249,6 +249,7 @@ export(data_remove) export(data_rename) export(data_rename_rows) export(data_reorder) +export(data_replicate) export(data_restoretype) export(data_rotate) export(data_seek) diff --git a/NEWS.md b/NEWS.md index d78e81ee9..6fa9b1219 100644 --- a/NEWS.md +++ b/NEWS.md @@ -12,6 +12,9 @@ NEW FUNCTIONS * `data_summary()`, to compute summary statistics of (grouped) data frames. +* `data_replicate()`, to expand a data frame by replicating rows based on another + variable that contains the counts of replications per row. + CHANGES * `data_modify()` gets three new arguments, `.at`, `.if` and `.modify`, to modify diff --git a/R/data_replicate.R b/R/data_replicate.R new file mode 100644 index 000000000..4ea6f998e --- /dev/null +++ b/R/data_replicate.R @@ -0,0 +1,120 @@ +#' @title Expand (i.e. replicate rows) a data frame +#' @name data_replicate +#' +#' @description +#' Expand a data frame by replicating rows based on another variable that +#' contains the counts of replications per row. +#' +#' @param data A data frame. +#' @param expand The name of the column that contains the counts of replications +#' for each row. Can also be a numeric value, indicating the position of that +#' column. Note that the variable indicated by `expand` must be an integer vector. +#' @param remove_na Logical. If `TRUE`, missing values in the column +#' provided in `expand` are removed from the data frame. If `FALSE` and `expand` +#' contains missing values, the function will throw an error. +#' @param ... Currently not used. +#' @inheritParams find_columns +#' +#' @return A dataframe with each row replicated as many times as defined in `expand`. +#' +#' @examples +#' data(mtcars) +#' data_replicate(head(mtcars), "carb") +#' @export +data_replicate <- function(data, + expand = NULL, + select = NULL, + exclude = NULL, + remove_na = FALSE, + ignore_case = FALSE, + verbose = TRUE, + regex = FALSE, + ...) { + # we need a name for the new column + if (is.null(expand)) { + insight::format_error( + "No column that should be used to expand the data frame was provided. Please use `expand` to define a column." + ) + } + + # only one column name + if (length(expand) > 1) { + insight::format_error( + "Please provide only a single string for `expand`, no character vector with multiple values." + ) + } + + # check if numerics, and if so, use column name + if (is.numeric(expand)) { + expand <- colnames(data)[expand] + } + + # check if in data + if (!expand %in% colnames(data)) { + insight::format_error( + "The column provided in `expand` does not exist in the data frame.", + .misspelled_string(colnames(data), expand, "Possibly misspelled?") + ) + } + + # check that "expand" contains no Inf + if (any(is.infinite(data[[expand]]))) { + insight::format_error( + "The column provided in `expand` contains infinite values. Please provide a column that does not contain infinite values." # nolint + ) + } + + # check that "expand" is integer + if (!.is_integer(data[[expand]])) { + insight::format_error( + "The column provided in `expand` is not of type integer. Please provide a column that contains integer values." # nolint + ) + } + + # evaluate select/exclude, may be select-helpers + select <- .select_nse(select, + data, + exclude, + ignore_case, + regex = regex, + verbose = verbose + ) + + # extract variable that contains the counts of replicates + replicates <- data[[expand]] + # we can remove that column now + data[[expand]] <- NULL + + # also remove "expand" from "select" string + select <- setdiff(select, expand) + + # if user doesn't want to remove "NA", but replicates contain "NA", + # give informative error here + if (!remove_na && anyNA(replicates)) { + insight::format_error( + "The column provided in `expand` contains missing values, but `remove_na` is set to `FALSE`.", + "Please set `remove_na` to `TRUE` or remove the missing values from the `expand` variable." + ) + } + + # remove rows where "expand" is NA + data <- data[!is.na(replicates), ] + replicates <- replicates[!is.na(replicates)] + + # fin + as.data.frame(do.call(cbind, lapply(data[select], rep.int, times = replicates))) +} + + +# is.integer(c(1, 2)) -> FALSE +# all(c(1, 2) %% 1 == 0) -> TRUE +.is_integer <- function(x, remove_na = TRUE) { + if (remove_na) { + x <- x[!is.na(x)] + } + tryCatch( + all(x %% 1 == 0), + warning = function(w) is.integer(x), + error = function(e) FALSE + ) +} diff --git a/_pkgdown.yaml b/_pkgdown.yaml index 0062b8a5a..65bae30c8 100644 --- a/_pkgdown.yaml +++ b/_pkgdown.yaml @@ -20,6 +20,7 @@ reference: - data_partition - data_rotate - data_group + - data_replicate - data_duplicated - data_unique diff --git a/man/data_replicate.Rd b/man/data_replicate.Rd new file mode 100644 index 000000000..4c152b371 --- /dev/null +++ b/man/data_replicate.Rd @@ -0,0 +1,93 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/data_replicate.R +\name{data_replicate} +\alias{data_replicate} +\title{Expand (i.e. replicate rows) a data frame} +\usage{ +data_replicate( + data, + expand = NULL, + select = NULL, + exclude = NULL, + remove_na = FALSE, + ignore_case = FALSE, + verbose = TRUE, + regex = FALSE, + ... +) +} +\arguments{ +\item{data}{A data frame.} + +\item{expand}{The name of the column that contains the counts of replications +for each row. Can also be a numeric value, indicating the position of that +column. Note that the variable indicated by \code{expand} must be an integer vector.} + +\item{select}{Variables that will be included when performing the required +tasks. Can be either +\itemize{ +\item a variable specified as a literal variable name (e.g., \code{column_name}), +\item a string with the variable name (e.g., \code{"column_name"}), or a character +vector of variable names (e.g., \code{c("col1", "col2", "col3")}), +\item a formula with variable names (e.g., \code{~column_1 + column_2}), +\item a vector of positive integers, giving the positions counting from the left +(e.g. \code{1} or \code{c(1, 3, 5)}), +\item a vector of negative integers, giving the positions counting from the +right (e.g., \code{-1} or \code{-1:-3}), +\item one of the following select-helpers: \code{starts_with()}, \code{ends_with()}, +\code{contains()}, a range using \code{:} or \code{regex("")}. \code{starts_with()}, +\code{ends_with()}, and \code{contains()} accept several patterns, e.g +\code{starts_with("Sep", "Petal")}. +\item or a function testing for logical conditions, e.g. \code{is.numeric()} (or +\code{is.numeric}), or any user-defined function that selects the variables +for which the function returns \code{TRUE} (like: \code{foo <- function(x) mean(x) > 3}), +\item ranges specified via literal variable names, select-helpers (except +\code{regex()}) and (user-defined) functions can be negated, i.e. return +non-matching elements, when prefixed with a \code{-}, e.g. \code{-ends_with("")}, +\code{-is.numeric} or \code{-(Sepal.Width:Petal.Length)}. \strong{Note:} Negation means +that matches are \emph{excluded}, and thus, the \code{exclude} argument can be +used alternatively. For instance, \code{select=-ends_with("Length")} (with +\code{-}) is equivalent to \code{exclude=ends_with("Length")} (no \code{-}). In case +negation should not work as expected, use the \code{exclude} argument instead. +} + +If \code{NULL}, selects all columns. Patterns that found no matches are silently +ignored, e.g. \code{find_columns(iris, select = c("Species", "Test"))} will just +return \code{"Species"}.} + +\item{exclude}{See \code{select}, however, column names matched by the pattern +from \code{exclude} will be excluded instead of selected. If \code{NULL} (the default), +excludes no columns.} + +\item{remove_na}{Logical. If \code{TRUE}, missing values in the column +provided in \code{expand} are removed from the data frame. If \code{FALSE} and \code{expand} +contains missing values, the function will throw an error.} + +\item{ignore_case}{Logical, if \code{TRUE} and when one of the select-helpers or +a regular expression is used in \code{select}, ignores lower/upper case in the +search pattern when matching against variable names.} + +\item{verbose}{Toggle warnings.} + +\item{regex}{Logical, if \code{TRUE}, the search pattern from \code{select} will be +treated as regular expression. When \code{regex = TRUE}, select \emph{must} be a +character string (or a variable containing a character string) and is not +allowed to be one of the supported select-helpers or a character vector +of length > 1. \code{regex = TRUE} is comparable to using one of the two +select-helpers, \code{select = contains("")} or \code{select = regex("")}, however, +since the select-helpers may not work when called from inside other +functions (see 'Details'), this argument may be used as workaround.} + +\item{...}{Currently not used.} +} +\value{ +A dataframe with each row replicated as many times as defined in \code{expand}. +} +\description{ +Expand a data frame by replicating rows based on another variable that +contains the counts of replications per row. +} +\examples{ +data(mtcars) +data_replicate(head(mtcars), "carb") +} diff --git a/tests/testthat/test-data_replicate.R b/tests/testthat/test-data_replicate.R new file mode 100644 index 000000000..92c514b5d --- /dev/null +++ b/tests/testthat/test-data_replicate.R @@ -0,0 +1,54 @@ +test_that("data_replicate: simple use case", { + data(mtcars) + d <- head(mtcars) + out <- data_replicate(d, "carb") + expect_identical(dim(out), c(13L, 10L)) + expect_identical(out$disp, c(160, 160, 160, 160, 160, 160, 160, 160, 108, 258, 360, 360, 225)) + expect_named(out, c("mpg", "cyl", "disp", "hp", "drat", "wt", "qsec", "vs", "am", "gear")) + + out <- data_replicate(d, 11) + expect_identical(dim(out), c(13L, 10L)) + expect_identical(out$disp, c(160, 160, 160, 160, 160, 160, 160, 160, 108, 258, 360, 360, 225)) + expect_named(out, c("mpg", "cyl", "disp", "hp", "drat", "wt", "qsec", "vs", "am", "gear")) + + d$mpg[5] <- NA + out <- data_replicate(d, "carb") + expect_identical(dim(out), c(13L, 10L)) + expect_identical(out$mpg, c(21, 21, 21, 21, 21, 21, 21, 21, 22.8, 21.4, NA, NA, 18.1)) + expect_named(out, c("mpg", "cyl", "disp", "hp", "drat", "wt", "qsec", "vs", "am", "gear")) + + d$carb[3] <- NA + out <- data_replicate(d, "carb", remove_na = TRUE) + expect_identical(dim(out), c(12L, 10L)) + expect_identical(out$mpg, c(21, 21, 21, 21, 21, 21, 21, 21, 21.4, NA, NA, 18.1)) + expect_named(out, c("mpg", "cyl", "disp", "hp", "drat", "wt", "qsec", "vs", "am", "gear")) + + out <- data_replicate(d, "carb", select = c("disp", "hp"), remove_na = TRUE) + expect_identical(dim(out), c(12L, 2L)) + expect_identical(out$disp, c(160, 160, 160, 160, 160, 160, 160, 160, 258, 360, 360, 225)) + expect_named(out, c("disp", "hp")) + + d <- data.frame( + a = c("a", "b", "c"), + b = 1:3, + rep = c(3, 2, 4), + stringsAsFactors = FALSE + ) + out <- data_replicate(d, "rep") + expect_identical(out$a, c("a", "a", "a", "b", "b", "c", "c", "c", "c")) +}) + + +test_that("data_replicate: errors", { + data(mtcars) + d <- head(mtcars) + expect_error(data_replicate(d), regex = "No column") + expect_error(data_replicate(d, expand = c("mpg", "gear")), regex = "a single string") + expect_error(data_replicate(d, expand = "geas"), regex = "The column provided") + expect_error(data_replicate(d, expand = "qsec"), regex = "The column provided") + d$carb[3] <- NA + expect_error(data_replicate(d, "carb"), regex = "missing values") + d <- head(mtcars) + d$carb[3] <- Inf + expect_error(data_replicate(d, "carb"), regex = "infinite values") +})