R/convert_to_percentage.R

Defines functions convert_to_percentage

Documented in convert_to_percentage

#' convert_to_percentage
#'
#' convert_to_percentage can convert a data frame to percentage.
#'
#' @param df A data frame.
#'
#' @param row_sum Default is TRUE. If row_sum == TRUE, then will take every value of a row and
#' divide by the summary of this row, and apply this to every row. If row_sum == FALSE, then will
#' take every value of a column and divide by the summary of this column, and apply this to every
#' column. Total sum of the result is 1.
#'
#' @param hundred_percent Default is FALSE, if TRUE, turn value to 100 percent, total sum is 100.
#'
#' @export
#'
#' @examples
#' convert_to_percentage(demo_dada2_result$seq_tab, row_sum = TRUE) %>% .[,1:5]

convert_to_percentage <- function(df, row_sum = TRUE, hundred_percent = FALSE) {
  # df must be data frame only contain numeric
  if (row_sum) {
    # 将df中每一行求和,然后用每一行中的每一个数字除以行和
    df <- sweep(df, 1, rowSums(df), '/')
  } else {
    # 将df中每一列求和,然后用每一列中的每一个数字除以列和
    df <- sweep(df, 2, colSums(df), '/')
  }
  if(hundred_percent) {
    return(as.data.frame(df * 100))
  } else {
    return(as.data.frame(df))
  }
}
yeguanhuav/visual16S documentation built on Feb. 19, 2022, 10:32 a.m.