R/summarize_plates.R

Defines functions se summarize_plates

Documented in se summarize_plates

#' Standard error
#'
#' Calculates the standard error of a set of measurements
#'
#' @param x Vector for which to calculate standard error.
#' @param na.rm Whether to remove \code{NA} values.
#'
#' @return The standard error of the vector.
#' @importFrom stats var na.omit
#'
#' @examples
#' x <- rnorm(5); se(x)
#' se(mtcars$mpg, T)
se <- function(x, na.rm = F) {
  if (na.rm) {
    x %<>% na.omit
  }
  sqrt(var(x) / length(x))
}
#' Summarize results
#'
#' Each \code{Formulation} will be summarized and given a \code{mean}, an \code{se}, and an \code{n} column.
#'
#' This should be the terminal step of analysis (which is why it removes other independent variables).
#'
#' @param df The data frame to summarize.
#' @param src.var The variable to be summarized (default: \code{Normalized}).
#'
#' @return A summary of the data frame, with \code{mean}, \code{se}, and \code{n} columns added based on \code{src.var}.
#' @export
#' @importFrom magrittr %>%
#' @importFrom dplyr enquo group_by n first
#'
#' @examples
#' some.df %>% normalize_plates %>% summarize_plates
#' some.df %>% pool_plates %>% normalize_plates %>% summarize_plates
summarize_plates <- function(df, src.var = Normalized) {
  src.var <- dplyr::enquo(src.var)
  df %>%
    group_by(Formulation) %>%
    summarize(Core = first(Core), # Formulation is implicitly retained
              DOPE = first(DOPE),
              Chol = first(Chol),
              PEG = first(PEG),
              mean = mean(!! src.var),
              se = se(!! src.var),
              n = n())
}
Aehmlo/lucy documentation built on Oct. 30, 2019, 4:09 a.m.