R/babble_ratio.R

Defines functions babble_ratio

Documented in babble_ratio

#' canonical babbling ratio
#'
#' proportion of canonical utterances (out of the sum of canonical and
#' non-canonical)
#'
#' @param xfile character, file path to rttm generated by
#' \code{\link{divime_classify_vox}}
#'
#' @details if the rttm is empty (i.e. contains no child vocalizations at all),
#' the returned list will contain only \code{NA}s. If there were some child
#' vocalizations but none of them were CNS or NCS, the counts in the output will
#' be 0 and only the ratio is returned as \code{NA}.
#'
#' @return a list with three items (\code{cns}: number of canonical utterances,
#' \code{ncs}: number of non-canonical utterances, \code{babble_ratio}: the
#' ratio)
#' @export

babble_ratio <- function(xfile) {
  # ignore empty files
  if (length(scan(xfile, quiet = TRUE, what = "raw")) > 0) {
    xdata <- read_rttm(xfile)

    cns <- sum(xdata$tier == "CNS")
    ncs <- sum(xdata$tier == "NCS")
    if (cns + ncs > 0) {
      babble_ratio <- cns / (cns + ncs)
      return(list(cns = cns, ncs = ncs, babble_ratio = babble_ratio))
    } else {
      return(list(cns = cns, ncs = ncs, babble_ratio = NA))
    }
  } else {
    return(list(cns = NA, ncs = NA, babble_ratio = NA))
  }
}
gobbios/avutils documentation built on Feb. 19, 2020, 9:44 a.m.