File: vec.R

package info (click to toggle)
r-cran-lava 1.8.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,816 kB
  • sloc: sh: 13; makefile: 2
file content (26 lines) | stat: -rw-r--r-- 746 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
##' vec operator
##'
##' Convert array into vector
##' @title vec operator
##' @param x Array
##' @param matrix If TRUE a row vector (matrix) is returned
##' @param sep Seperator
##' @param ... Additional arguments
##' @author Klaus Holst
##' @export
vec <- function(x,matrix=FALSE,sep=".",...) {
    if (is.vector(x) && !is.list(x)) {
        res <- x
    } else if (is.list(x)) {
        res <- stats::setNames(unlist(x),names(x))
    } else {
        if (is.matrix(x) && is.null(rownames(x))) {
            nn <- colnames(x)
        } else {
            nn <- apply(expand.grid(dimnames(x)),1,function(x) paste(x,collapse=sep))
        }
        res <- as.vector(x); names(res) <- nn
    }
    if (matrix) return(cbind(res))
    return(res)
}