R/stat.r

library(R6)

#' Handy Statistics
#'
#' @export

Stat <- R6Class("Stat",

  public=list(
    digits=NA,
    nsmall=NA,
    initialize = function(digits=2, nsmall=2){
      self$digits <- digits
      self$nsmall <- nsmall
    },
    preety = function(n){
      format(n, digits=self$digits, nsmall=self$nsmall)
    },
    avg = function(df, col){
      self$preety(mean(df[,col], na.rm=TRUE))
    },
    std = function(df, col){
      self$preety(sd(df[,col], na.rm=TRUE))
    },
    sum = function(df, col){
      self$preety(sum(df[,col], na.rm=TRUE))
    },
    nopp = function(df, col){
      NULL
    },
    blank = function(df, col){
      " "
    },
    id = function(df, col){
      df[1, col]
    },
    p50 = function(df, col){
      self$preety(median(df[,col], na.rm=TRUE))
    },
    iqr = function(df, col){
      self$preety(IQR(df[,col], na.rm=TRUE))
    },
    prev = function(n, t){
      self$preety(n / t * 100)
    },
    count = function(df, col){
      format(sum(!is.na(df[,col])), digits=0, nsmall=0)
    }
  )
)
tiagotaveiragomes/rigma.sheet documentation built on Aug. 9, 2020, 12:35 a.m.