From a9f0ec6c74a7ae286b5809b37c7499b890a30d21 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 22 Mar 2024 22:39:32 +0100 Subject: [PATCH] rename --- NEWS.md | 2 +- R/{data_expand.R => data_replicate.R} | 10 ++++------ _pkgdown.yaml | 2 +- ...st-data_expand.R => test-data_replicate.R} | 20 +++++++++---------- 4 files changed, 16 insertions(+), 18 deletions(-) rename R/{data_expand.R => data_replicate.R} (91%) rename tests/testthat/{test-data_expand.R => test-data_replicate.R} (62%) diff --git a/NEWS.md b/NEWS.md index 2e806b9ac..6fa9b1219 100644 --- a/NEWS.md +++ b/NEWS.md @@ -12,7 +12,7 @@ NEW FUNCTIONS * `data_summary()`, to compute summary statistics of (grouped) data frames. -* `data_expand()`, to expand a data frame by replicating rows based on another +* `data_replicate()`, to expand a data frame by replicating rows based on another variable that contains the counts of replications per row. CHANGES diff --git a/R/data_expand.R b/R/data_replicate.R similarity index 91% rename from R/data_expand.R rename to R/data_replicate.R index c83389124..8e59cb9e3 100644 --- a/R/data_expand.R +++ b/R/data_replicate.R @@ -1,5 +1,5 @@ #' @title Expand (i.e. replicate rows) a data frame -#' @name data_expand +#' @name data_replicate #' #' @description #' Expand a data frame by replicating rows based on another variable that @@ -18,9 +18,9 @@ #' #' @examples #' data(mtcars) -#' data_expand(head(mtcars), "carb") +#' data_replicate(head(mtcars), "carb") #' @export -data_expand <- function(data, +data_replicate <- function(data, expand = NULL, select = NULL, exclude = NULL, @@ -82,7 +82,5 @@ data_expand <- function(data, replicates <- replicates[!is.na(replicates)] # fin - as.data.frame(do.call(cbind, lapply(data[select], function(variable) { - unlist(Map(rep, variable, replicates), use.names = FALSE) - }))) + as.data.frame(do.call(cbind, lapply(data[select], rep.int, times = replicates))) } diff --git a/_pkgdown.yaml b/_pkgdown.yaml index 3dfeeefb9..65bae30c8 100644 --- a/_pkgdown.yaml +++ b/_pkgdown.yaml @@ -20,7 +20,7 @@ reference: - data_partition - data_rotate - data_group - - data_expand + - data_replicate - data_duplicated - data_unique diff --git a/tests/testthat/test-data_expand.R b/tests/testthat/test-data_replicate.R similarity index 62% rename from tests/testthat/test-data_expand.R rename to tests/testthat/test-data_replicate.R index 8122d8ee9..17be2dd89 100644 --- a/tests/testthat/test-data_expand.R +++ b/tests/testthat/test-data_replicate.R @@ -1,35 +1,35 @@ -test_that("data_expand: simple use case", { +test_that("data_replicate: simple use case", { data(mtcars) d <- head(mtcars) - out <- data_expand(d, "carb") + 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")) d$mpg[5] <- NA - out <- data_expand(d, "carb") + 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_expand(d, "carb", remove_na = TRUE) + 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_expand(d, "carb", select = c("disp", "hp"), remove_na = TRUE) + 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")) }) -test_that("data_expand: errors", { +test_that("data_replicate: errors", { data(mtcars) d <- head(mtcars) - expect_error(data_expand(d), regex = "No column") - expect_error(data_expand(d, expand = c("mpg", "gear")), regex = "a single string") - expect_error(data_expand(d, expand = "geas"), regex = "The column provided") + 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") d$carb[3] <- NA - expect_error(data_expand(d, "carb"), regex = "missing values") + expect_error(data_replicate(d, "carb"), regex = "missing values") })