Skip to content

Commit

Permalink
add helper function for percentile normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
berntpopp committed Oct 31, 2023
1 parent a2077f1 commit b255c1c
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions analyses/functions/helper-functions.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@


require(dplyr)
require(tibble)

#' Percentile Normalization of a Column in a Tibble
#'
#' This function normalizes a specified column in a tibble by transforming each value
#' to the percentile in which it falls relative to the entire column.
#'
#' @param data A tibble containing the data.
#' @param colname A string representing the name of the column to be normalized.
#'
#' @return A tibble with an additional column named "<colname>_percentile" where each
#' entry represents the percentile of the corresponding value in the original
#' column. Percentile values range from 0 to 1.
#'
#' @examples
#' library(tibble)
#' library(dplyr)
#'
#' # Example tibble
#' df <- tibble::tibble(
#' approved_symbol = c("ABCG2", "ACE", "ACTA2"),
#' source_count = c(3, 10, 6)
#' )
#'
#' # Applying normalization
#' df_normalized <- normalize_percentile(df, "source_count")
#'
#' @export
normalize_percentile <- function(data, colname) {
data %>%
mutate("{colname}_percentile" := rank(!!sym(colname), ties.method = "average") / n())
}

0 comments on commit b255c1c

Please sign in to comment.