diff --git a/NAMESPACE b/NAMESPACE index 31b7b11..e07d1e5 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -6,6 +6,7 @@ export(distinctiveness_com) export(distinctiveness_stack) export(funrar) export(funrar_stack) +export(make_relative) export(matrix_to_stack) export(restrictedness) export(restrictedness_stack) diff --git a/R/make_relative.R b/R/make_relative.R new file mode 100644 index 0000000..66e2621 --- /dev/null +++ b/R/make_relative.R @@ -0,0 +1,31 @@ +#' Relative abundance matrix from absolute abundance matrix +#' +#' From an abundance matrix (numbers of individuals of a given species at a site) +#' returns a relative abundance matrix (proportion of individuals of a given species +#' at a given site). This function works also with sparse matrices. +#' +#' @param abund_matrix abundance matrix, with sites in rows and species in columns +#' +#' @export +make_relative = function(abund_matrix) { + + # Check input type using internal function + check_matrix(abund_matrix, "abundance") + + # Compute relative abundances matrix + if (requireNamespace("Matrix", quietly = TRUE) & + is(abund_matrix, "sparseMatrix")) { + + sites_abund = Matrix::rowSums(abund_matrix, na.rm = TRUE) + + rel_abund_matrix = abund_matrix / sites_abund + } else { + # Compute total site abundances + sites_abund = rowSums(abund_matrix, na.rm = TRUE) + + # Divide each individual abundace by total site abundance + rel_abund_matrix = sweep(abund_matrix, 1, sites_abund, "/") + } + + return(rel_abund_matrix) +} diff --git a/man/make_relative.Rd b/man/make_relative.Rd new file mode 100644 index 0000000..4243097 --- /dev/null +++ b/man/make_relative.Rd @@ -0,0 +1,17 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/make_relative.R +\name{make_relative} +\alias{make_relative} +\title{Relative abundance matrix from absolute abundance matrix} +\usage{ +make_relative(abund_matrix) +} +\arguments{ +\item{abund_matrix}{abundance matrix, with sites in rows and species in columns} +} +\description{ +From an abundance matrix (numbers of individuals of a given species at a site) +returns a relative abundance matrix (proportion of individuals of a given species +at a given site). This function works also with sparse matrices. +} + diff --git a/tests/testthat/test-rel_abund.R b/tests/testthat/test-rel_abund.R new file mode 100644 index 0000000..0689b3a --- /dev/null +++ b/tests/testthat/test-rel_abund.R @@ -0,0 +1,37 @@ +# Test file for function to convert abundance matrix to relative abundances +# matrix + +context("Abundances to Relative abundances Matrix") + +# Packages --------------------------------------------------------------------- +library(Matrix) + +# Common objects --------------------------------------------------------------- + +# Abundance Matrix to test +abs_abund_mat = matrix(c(1, 1, 1, NA, + NA, rep(1, 3), + NA, 1, 1, NA, + NA, NA, 1, 1), + ncol = 4) + +sparse_abs_abund = as(abs_abund_mat, "sparseMatrix") + +# Corresponding relative abundances matrix +rel_abund_mat = matrix(c(1, 1/3, 1/4, NA, + NA, 1/3, 1/4, 1/2, + NA, 1/3, 1/4, NA, + NA, NA, 1/4, 1/2), + ncol = 4) + +sparse_rel_abund = as(rel_abund_mat, "sparseMatrix") + +# Tests ------------------------------------------------------------------------ + +test_that("Can convert from absolute to relative abundances matrices", { + # On regular matrices + expect_equal(make_relative(abs_abund_mat), rel_abund_mat) + + # On sparse matrices + expect_equal(make_relative(sparse_abs_abund), sparse_rel_abund) +})