R/getCharFrequency.R

Defines functions getCharFrequency

Documented in getCharFrequency

#' getCharFrequency
#'
#' Calculate the char frequency (as a percentage of total chars)
#' for the given text (i.e. email)
#' @param text Text where the char frequency will be calculated from
#' @param chars Vector of characters that will be included in the result
#' @return Vector of the char frequency (i.e. same length as the 'chars' param)
#' @examples
#' getCharFrequency("This is my text", c("i", "t", "?"))
#'

getCharFrequency <- function(text, chars) {
  charFreq <- table(unlist(strsplit(text, "")))
  charFreqVector <- vapply(chars, function(char){
    count <- charFreq[names(charFreq)==char]
    if (length(count) == 0) { count <- 0 }
    return(count)
  }, double(1))
  totalChars = nchar(text)
  charFreqVector <- charFreqVector * 100 / totalChars
  return(charFreqVector)
}
megahf/spamfilter documentation built on May 29, 2019, 4:42 a.m.