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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
#' Array to vector conversion
#'
#' Convert an array into a vector.
#'
#' This function converts an array (or a multi-way contingency table) into a
#' vector, using a consistent ordering of the cells. The ordering of the cells
#' is lexicographical and cannot be specified by the user.
#'
#' @param tab An array of counts
#' @return a Named integer vector. The names correspond to the cell indices in
#' the table.
#' @export
#' @seealso [vec2tab()]
#' @examples
#'
#' a <- array(1:6, c(1,2,3))
#' tab2vec(a)
#'
#' data(Titanic)
#' tab2vec(Titanic)
#' Titanic[1,1,1,1]
#' Titanic[1,1,1,2]
#'
#'
tab2vec <- function(tab){
# if is a vector, return
if(is.null(dim(tab))){
tab <- as.vector(tab)
names(tab) <- 1:length(tab)
return(tab)
}
# if it's a vector already, short-circuit
if(length(dim(tab)) == 1){
tab <- as.vector(tab)
names(tab) <- 1:length(tab)
return(tab)
}
# otherwise, rotate and class
u <- aperm(tab, length(dim(tab)):1)
if(inherits(tab[1], "numeric")) u <- as.vector(u)
if(inherits(tab[1], "integer")) u <- as.integer(u)
# create cell indices
tmpdf <- expand.grid(
rev.default(lapply(dim(tab), function(x) 1:x))
)[,length(dim(tab)):1]
# assign them as names to u
names(u) <- apply(tmpdf, 1, paste, collapse = ',')
# return
u
}
|