R/list-methods.R

Defines functions suml meanl varl sdl

#' Sum, mean, var and sd functions for lists of objects
#' 
#' @usage
#' suml(object)
#' meanl(object)
#' varl(object)
#' sdl(object)
#'
#' @examples
#' 
#' set.seed(123)
#' 
#' x <- list(matrix(rnorm(9), 3, 3),
#'           matrix(rnorm(9), 3, 3),
#'           matrix(rnorm(9), 3, 3))
#' suml(x)
#' meanl(x)
#' varl(x)
#' sdl(x)
#' 
#' @name list-methods
NULL


#' @export

suml <- function(object) {
   out <- object[[1]]
   for (i in 2:length(object)) {
      out <- out + object[[i]]
   }
   out
}


#' @export

meanl <- function(object) suml(object)/length(object)


#' @export

varl <- function(object) {
   n <- length(object)
   m <- suml(object)/n
   
   out <- (object[[1]] - m)^2
   for (i in 2:length(object)) {
      out <- out + (object[[i]] - m)^2
   }
   out/(n-1)
}


#' @export
 
sdl <- function(object) sqrt(varl(object))
twolodzko/twextras documentation built on May 3, 2019, 1:52 p.m.