table2df: Convert Matrix to Data Frame

Usage Arguments Examples

View source: R/core_mutSignatures_scr_5.R

Usage

1
table2df(dataMatrix, rowLab = "sample", colLab = "feature", valueLab = "count")

Arguments

dataMatrix
rowLab
colLab
valueLab

Examples

 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
##---- Should be DIRECTLY executable !! ----
##-- ==>  Define data, use random,
##--	or do  help(data=index)  for the standard data sets.

## The function is currently defined as
function (dataMatrix, rowLab = "sample", colLab = "feature", 
    valueLab = "count") 
{
    if (!is.null(colnames(dataMatrix))) {
        names.X <- colnames(dataMatrix)
    }
    else {
        names.X <- 1:ncol(dataMatrix)
    }
    if (!is.null(rownames(dataMatrix))) {
        names.Y <- rownames(dataMatrix)
    }
    else {
        names.Y <- 1:nrow(dataMatrix)
    }
    tmp <- do.call(rbind, lapply(1:nrow(dataMatrix), (function(i) {
        t(sapply(1:ncol(dataMatrix), (function(j) {
            c(names.Y[i], names.X[j], dataMatrix[i, j])
        })))
    })))
    tmp <- data.frame(tmp, stringsAsFactors = FALSE)
    if (is.numeric(dataMatrix[1, 1])) 
        tmp[, 3] <- as.numeric(as.character(tmp[, 3]))
    rownames(tmp) <- NULL
    colnames(tmp) <- c(rowLab, colLab, valueLab)
    return(tmp)
  }

dami82/mutSignatures_dev documentation built on May 17, 2019, 7:02 p.m.