#' Cumulative counts for cumulative distribution
#'
#' This function calculates the cumulative counts for the the cumulative distribution given a vector of non-cumulative counts as input.
#'
#' The non-cumulative counts can be obtained with R's hist(x, plot=FALSE) function.
#' The cumulation of counts can be specified to be from left-to-right or right-to-left of the vector.
#'
#' @param vecOfCounts The vector of non-cumulative counts. Can be generated by hist() function.
#' @param plot A boolean to indicate whether to perform cumulative count accumulation left-to-right, i.e. forward, (defult) or right-to-left, i.e. backwards. Defults to FALSE.
#' @export
acumulated.distrib <- function(vecOfCounts, backwards = FALSE) {
largest_so_far <- 0
out <- vector()
if(backwards == FALSE){
for(i in 1:length(vecOfCounts)){
toBeAdded <- vecOfCounts[i] + largest_so_far
largest_so_far <- largest_so_far + vecOfCounts[i]
out <- c(out, toBeAdded)
}
}
else{
vecOfCounts <- rev(vecOfCounts)
for(i in 1:length(vecOfCounts)){
toBeAdded <- vecOfCounts[i] + largest_so_far
largest_so_far <- largest_so_far + vecOfCounts[i]
out <- c(out, toBeAdded)
}
out <- rev(out)
}
out
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.