R/matrix-utils.R

Defines functions mat2list mat2df

Documented in mat2df mat2list

#' convert a matrix to a tidy data frame
#' (from wide to long format as described in the tidyverse concept)
#'
#'
#' @title mat2df
#' @param x the input matrix
#' @return a data.frame in long format with the 'value' column stores the original values
#' and 'row' and 'col' columns stored in row and column index as in x
#' @examples
#' x <- matrix(1:15, nrow = 3)
#' mat2df(x)
#' @export
#' @author Guangchuang Yu
mat2df <- function(x) {
    nr <- nrow(x)
    nc <- ncol(x)
    d <- data.frame(
        value = as.vector(x),
        row = rep(1:nr, times = nc),
        col = rep(1:nc, each = nr)
    )
    return(d)
}

#' convert a matrix to a list
#'
#'
#' @title mat2list
#' @param x the input matrix
#' @return a list that contains matrix columns as its elements
#' @examples
#' x <- matrix(1:15, nrow = 3)
#' mat2list(x)
#' @export
mat2list <- function(x){
  lapply(seq_len(ncol(x)), function(i) x[,i])
}

Try the yulab.utils package in your browser

Any scripts or data that you put into this service are public.

yulab.utils documentation built on Feb. 5, 2026, 9:10 a.m.