R/fmakeVar.R

Defines functions fmakeVar

Documented in fmakeVar

#' Function to apply a variable table to a baseData object
#' 
#' Returns the time indes to use for initialisation
#' 
#' @param baseData an xts object with column names being variables
#' @param varTbl a matrix of weights with named columns and rows. The variables named after rows are created by weighted sums of the columns variables.
#' 
#' @keywords FloorForT
#' @export
#' @examples
#' # Not Run
#' # ModelBuild()
fmakeVar <- function(baseData,varTbl){
    if( !all(colnames(varTbl) %in% names(baseData)) ){
        stop("Missing variables in the baseData")
    }
    out <- matrix(0,nrow(baseData),nrow(varTbl))
    colnames(out) <- rownames(varTbl)
    for(ii in rownames(varTbl)){
        w <- rep(0,nrow(baseData))
        for(jj in colnames(varTbl)){
            idx <- which(is.finite(baseData[,jj]))
            out[idx,ii] <- out[idx,ii] + varTbl[ii,jj]*baseData[idx,jj]
            w[idx] <- w[idx] + varTbl[ii,jj]
        }
        out[,ii] <- out[,ii] / w
    }
    out[!is.finite(out)] <- NA

    out <- xts(x = out,
               order.by = index(baseData),
               frequency = NULL,
               unique = TRUE,
               tzone = 'GMT')

    return(out)
}
waternumbers/FloodForT documentation built on Nov. 5, 2019, 12:07 p.m.