R/fqtable.R

Defines functions fqtable

Documented in fqtable

#' Tabulate and return relative frequencies of values
#'
#' @param vect input vector
#' @param scaling multiple frequencies by this scaling factor (e.g, 100 to get percentages)
#' @param hightolow should returned frequency table elements be sorted from high to low?
#' @param accumulate include a 'cummulative frequency' column
#' @param ... additional arguments passed to `table` function
#'
#' @return data.frame with unique values, counts, and frequencies
#' @importFrom readr parse_guess
#' @export
fqtable <- function(vect, scaling = 1, hightolow = TRUE, accumulate = FALSE, ... ) {

	#if(is.factor(vect)) vect = as.character(vect)

	tmptab = table(vect, ...)

	tmpdf = data.frame(
		value = parse_guess(names(tmptab)),
		n = unname(as.integer(tmptab)),
		freq = as.numeric((tmptab / sum(tmptab)) * scaling)
	)

	if(hightolow)
		tmpdf = tmpdf[order(tmpdf$freq, decreasing = TRUE),]

	if(accumulate){
		tmpdf['cfreq'] = cumsum(tmpdf$freq)
	}

	tmpdf
}
stackcon/rngt documentation built on June 17, 2022, 5:29 p.m.