SaveMatrix: Save a matrix to a text file

Description Usage Arguments Value Author(s) Examples

View source: R/SaveMatrix.R

Description

Write out a matrix to a text file with row and column labels

Usage

1
SaveMatrix(startYear, localMatrix, outFile)

Arguments

startYear

the year to use as the first row label

localMatrix

the matrix to be written out

outFile

the file name and path of the file to be created

Value

Returns the incoming matrix with an extra column added on the left edge containing the years.

Author(s)

Dave Conklin

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
## The function is currently defined as
function (startYear, localMatrix, outFile) 
{
    nSeq = dim(localMatrix)[2]
    stopifnot(nSeq >= 1)
    matrixRows = dim(localMatrix)[1]
    stopifnot(matrixRows > 1)
    indexedSeq = matrix(0, nrow = nSeq, ncol = matrixRows + 1)
    appendFlag = FALSE
    for (ndx in 1:nSeq) {
        cat(c(startYear + ndx - 1), file = outFile, append = appendFlag)
        appendFlag = TRUE
        indexedSeq[ndx, 1] = startYear + ndx - 1
        for (row in 1:matrixRows) {
            if (is.na(localMatrix[row, ndx])) 
                localMatrix[row, ndx] = 0
            cat(c(", ", localMatrix[row, ndx]), file = outFile, 
                append = appendFlag)
            indexedSeq[ndx, row + 1] = localMatrix[row, ndx]
        }
        cat(c("\n"), file = outFile, append = appendFlag)
    }
    return(indexedSeq)
  }

MC2toPath documentation built on May 29, 2017, 7:54 p.m.