-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
83f9703
commit c967b6c
Showing
4 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.