Skip to contents

This function will perform quantile normalization on two or more distributions of equal length. Quantile normalization is a technique used to make the distribution of values across different samples more similar. It ensures that the distributions of values for each sample have the same quantiles. This function takes a numeric matrix as input and returns a quantile-normalized matrix.

Usage

quantile_normalize(.data, .return_tibble = FALSE)

Arguments

.data

A numeric matrix where each column represents a sample.

.return_tibble

A logical value that determines if the output should be a tibble. Default is 'FALSE'.

Value

A numeric matrix (or tibble if .return_tibble = TRUE) that has been quantile normalized. Each column represents a sample, and the quantile normalization ensures that the distributions of values for each sample have the same quantiles.

Details

This function performs quantile normalization on a numeric matrix by following these steps:

  1. Sort each column of the input matrix.

  2. Calculate the mean of each row across the sorted columns.

  3. Replace each column's sorted values with the row means.

  4. Unsort the columns to their original order.

Author

Steven P. Sanderson II, MPH

Examples

# Create a sample numeric matrix
data <- matrix(rnorm(20), ncol = 4)

# Perform quantile normalization
normalized_data <- quantile_normalize(data)
normalized_data
#>             [,1]        [,2]        [,3]        [,4]
#> [1,]  1.54004626  1.54004626 -0.42145863 -0.42145863
#> [2,]  0.09638238  0.37319866  0.09638238  0.09638238
#> [3,] -1.50679669 -0.42145863  0.37319866  0.37319866
#> [4,] -0.42145863  0.09638238  1.54004626  1.54004626
#> [5,]  0.37319866 -1.50679669 -1.50679669 -1.50679669

as.data.frame(normalized_data) |>
  sapply(function(x) quantile(x, probs = seq(0, 1, 1 / 4)))
#>               V1          V2          V3          V4
#> 0%   -1.50679669 -1.50679669 -1.50679669 -1.50679669
#> 25%  -0.42145863 -0.42145863 -0.42145863 -0.42145863
#> 50%   0.09638238  0.09638238  0.09638238  0.09638238
#> 75%   0.37319866  0.37319866  0.37319866  0.37319866
#> 100%  1.54004626  1.54004626  1.54004626  1.54004626

quantile_normalize(
data.frame(rnorm(30),
           rnorm(30)),
           .return_tibble = TRUE)
#> # A tibble: 30 × 2
#>    rnorm.30. rnorm.30..1
#>        <dbl>       <dbl>
#>  1    -0.434      -0.694
#>  2     0.649      -0.445
#>  3    -1.88       -0.162
#>  4     0.683       0.263
#>  5    -1.14        0.600
#>  6    -0.298      -0.898
#>  7    -0.996      -0.996
#>  8    -1.52       -1.19 
#>  9     0.229       1.82 
#> 10    -0.898      -0.104
#> # ℹ 20 more rows