Skip to content

Commit

Permalink
Draft data_expand()
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Mar 20, 2024
1 parent 83f9703 commit c967b6c
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ export(data_adjust)
export(data_arrange)
export(data_codebook)
export(data_duplicated)
export(data_expand)
export(data_extract)
export(data_filter)
export(data_find)
Expand Down
69 changes: 69 additions & 0 deletions R/data_expand.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#' @title Expand (i.e. replicate rows) a data frame
#' @name data_expand
#'
#' @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.
#' @param ... Currently not used.
#' @inheritParams find_columns
#'
#' @return `data`, with each row replicated as many times as defined in `expand`.
#'
#' @examples
#' data(mtcars)
#' data_expand(head(mtcars), "carb")
#' @export
data_expand <- 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 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?")
)
}

# 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[[replicates]] <- NULL

# fin
as.data.frame(do.call(cbind, lapply(data[select], function(variable) {
unlist(Map(rep, variable, replicates), use.names = FALSE)
})))
}
1 change: 1 addition & 0 deletions _pkgdown.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ reference:
- data_partition
- data_rotate
- data_group
- data_expand
- data_duplicated
- data_unique

Expand Down
88 changes: 88 additions & 0 deletions man/data_expand.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c967b6c

Please sign in to comment.