R/split_byrow_bycol.R

Defines functions split_bycol

Documented in split_bycol

#' Split matrix or dataframe into list
#'
#' Split matrix or dataframe into list by columns or by rows
#'
#' @name split_byrow_bycol
#' @param x Matrix or dataframe.
#'
#' @export
#' @rdname split_byrow_bycol

split_bycol <- function(x){
    if (!inherits(x, c("matrix", "data.frame")))
        stop("'x' must be matrix or dataframe\n")

    if (ncol(x) == 0){
        return(list())
    }

    as.list(as.data.frame(x))
}

#' @export
#' @rdname split_byrow_bycol
split_byrow <- function (x) 
{
    if (!inherits(x, c("matrix", "data.frame"))) 
        stop("'x' must be matrix or dataframe\n")
    if (nrow(x) == 0) {
        return(list())
    }

    f <- 1:nrow(x)
    out <- split(x, f)
    
    if (!is.null(rownames(x))){ 
        names(out) <- rownames(x)        
    }
    return(out)
}


## split_byrow <- function(x){
    ## if (!inherits(x, c("matrix", "data.frame")))
        ## stop("'x' must be matrix or dataframe\n")

    ## if (nrow(x) == 0){
        ## return(list())
    ## }
    ## if (!is.null(rownames(x)))
        ## f <- rownames(x)
    ## else
        ## f <- 1:nrow(x)
    ## split(x, f)    
## }

Try the doBy package in your browser

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

doBy documentation built on Nov. 2, 2023, 5:48 p.m.