#' 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)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.