R/foldLibrary.R

foldLibrary <- function(DT_featureLibrary, cvFolds, seed){

    ## Error-checking inputs
    if(cvFolds <= 0 && round(cvFolds) != cvFolds){
        stop("cvFolds must be a whole number (and greater than 0)")
    }
    if(round(seed) != seed){
        stop("seed must be an integer")
    }

    ## Seed
    set.seed(seed)

    ## Find first column index of runs
    minimumCol <- min(grep("ExactMass", colnames(DT_featureLibrary)))
    keepCols <- 1:minimumCol

    ## Create random partitions based on 'cvFolds'
    folds <- createFolds((minimumCol+1):ncol(DT_featureLibrary), cvFolds)

    ## Initialize return list of data table partitions
    DT_return <- vector(mode = "list", length = length(folds))

    ## Store folds in return data table
    for(i in seq_along(folds)){
        folds[[i]] <- folds[[i]] + minimumCol
        folds[[i]] <- c(keepCols, folds[[i]])
        DT_return[[i]] <- DT_featureLibrary[, folds[[i]], with=F]
    }

    return(DT_return)
}
jchitpin/blistR documentation built on July 8, 2019, 6:29 p.m.