R/applyPerCondPerRow.R

Defines functions applyPerCondPerRow

Documented in applyPerCondPerRow

#' Apply a function per condition, per row, given data frame and annotation
#' 
#' This function allows application of user supplied function upon a data frame per condition specified in annotation and per row of the data frame.
#' 
#' @param df A data frame upon which we wish to apply the function.
#' @param anno An annotation (of data frame class) of the data frame. Ensure its rows correspond to columns of data frame. Ensure there is a column named "condition".
#' @param FUN A function to be applied per condition, per row
#' @param ... Ellipsis. Arguments to be supplied to the FUN.
#' @return A data frame after FUN application per condition, per row.
#' @export


applyPerCondPerRow <- function(df, anno, FUN, ...) {
  cases <- unique(anno$condition)
  out <- as.data.frame(matrix(nrow = nrow(df)))
  for(i in 1:length(cases)){
    logicalAtCase <- anno$condition==cases[i]
    condDF <- df[,logicalAtCase]
    print(paste("Processing case ", cases[i]))
    for(z in 1:nrow(condDF)){
      condDF[z,] <- FUN(condDF[z,], ...)
      }
    out <- cbind(out, condDF)
  }
out[,1] <- NULL
return(out)
}
msxakk89/dat documentation built on Aug. 3, 2020, 6:39 p.m.