Skip to content

Commit

Permalink
rename
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Mar 22, 2024
1 parent ec881c4 commit a9f0ec6
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 18 deletions.
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 4 additions & 6 deletions R/data_expand.R → R/data_replicate.R
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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)))
}
2 changes: 1 addition & 1 deletion _pkgdown.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ reference:
- data_partition
- data_rotate
- data_group
- data_expand
- data_replicate
- data_duplicated
- data_unique

Expand Down
Original file line number Diff line number Diff line change
@@ -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")
})

0 comments on commit a9f0ec6

Please sign in to comment.