R/absolute_to_percentage.R

Defines functions absolute_to_percentage

Documented in absolute_to_percentage

#' Converts absolute abundance data to a percentage of total abundance for each site
#'
#' @param data The dataframe to be used.
#' @param col The column that change is being measured on.
#' @param site The column containing the site of each sample.
#' @return The `data` dataframe with an added 'percentage' column.
#' @import dplyr
#' @export 
absolute_to_percentage <- function(data, col, site){

  #creates an empty table with the original table's columns, plus the percentage values
  #i'm aware this is incredibly hacky, but i couldn't find a better way
  results <- filter(cbind(data, hellinger_trans_vals = c(1:nrow(data))), col == "hack")
  #finds values for each site
  unique_sites <- unique(data[[site]])
  for(s in unique_sites){
    site_obs <- data[data[[site]] == s,]
    sum <- sum(site_obs[col], na.rm = TRUE)
    site_obs <- mutate(site_obs, percentage = (site_obs[col] * 100) / sum)
    #binds values for each site back into a copy of the original data set
    results <- rbind(results, site_obs)
  }
  return(results)
}

Try the rshift package in your browser

Any scripts or data that you put into this service are public.

rshift documentation built on April 12, 2025, 1:20 a.m.