R/as.matrix.R

Defines functions as.matrix.correlate

### as.matrix.R --- 
##----------------------------------------------------------------------
## Author: Brice Ozenne
## Created: jul  5 2024 (18:56) 
## Version: 
## Last-Updated: aug  8 2024 (09:54) 
##           By: Brice Ozenne
##     Update #: 16
##----------------------------------------------------------------------
## 
### Commentary: 
## 
### Change Log:
##----------------------------------------------------------------------
## 
### Code:

## * as.matrix.correlate
##' @export
as.matrix.correlate <- function(x, index, ...){

    ## ** check and normalize user imput
    ## *** dots
    dots <- list(...)
    if(length(dots)>0){
        stop("Unknown arguments \'",paste(names(dots), collapse = "\' \'"),"\'. \n")
    }

    ## ** subset
    if(!missing(index)){
        x.array <- as.array(x)
        if(length(index)>1){
            stop("Incorrect argument \'index\': should have length 1. \n")
        }else if(is.numeric(index) && index %in% 1:dim(x.array)[3] == FALSE){
            stop("Incorrect argument \'index\': when numeric it should take integer value between 1 and the number of correlation matrix (here ",dim(x.array)[3],"). \n")
        }else if(is.character(index) && index %in% dimnames(x.array)[[3]] == FALSE){
            stop("Incorrect argument \'index\': when character it should refer to the name of the correlation matrix. \n",
                 "Possible names: \"",paste(dimnames(x.array)[[3]], collapse = "\", \""),"\". \n")
        }
        out <- x[,,index]
    }else{
        if(length(x)>1 || length(x[[1]])>1){
            message("Only the first correlation matrix was extracted. \n")
        }
        out <- x[[1]][[1]]
    }

    ## ** export
    return(out)
}


##----------------------------------------------------------------------
### as.matrix.R ends here
bozenne/repeated documentation built on July 16, 2025, 11:16 p.m.