Skip to content

Instantly share code, notes, and snippets.

@tomsing1
Created July 7, 2021 21:20
Show Gist options
  • Select an option

  • Save tomsing1/6df5e3a9d98617eac85892b3d8056a59 to your computer and use it in GitHub Desktop.

Select an option

Save tomsing1/6df5e3a9d98617eac85892b3d8056a59 to your computer and use it in GitHub Desktop.
calculate the centered log ratio (CLR) for a data matrix (for compositional data)
#' Centered log-ratio transformation
#'
#' @param m Count matrix with features in rows and samples (cells) in columns
#' @return matrix
#' @export
clr <- function(m) {
apply(m, 2, function(x) {
log1p(x = x/(exp(x = sum(log1p(x = x[x > 0]), na.rm = TRUE)/length(x = x))))
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment