Description Usage Arguments Details Value Author(s) Examples
Change the size of a matrix by adding or subdividing its cells.
1 | reaggregate.matrix(mtx, eq, nrow=NULL, ncol=NULL)
|
mtx |
A matrix that will be refactored |
eq |
A |
nrow |
An explict number of rows in the result matrix; the default is to use the maximum value of |
ncol |
An explict number of columns in the result matrix; the default is to use the maximum value of |
reaggregate.matrix
will compute a new matrix with revised dimensions from an input matrix, mtx
, based on
the correspondences described in the equivalence table, eq
.
The equivalence table must be a data.frame
or matrix
containing the following columns:
The row index of an input matrix cell
The column index of an input matrix cell
The row index of an output matrix cell
The column index of an output matrix cell
The fraction of cell mtx[i, j]
to be placed in cell mtx.out[o, p]
Each row in the equivalence table describes how to move some or all of the value in the [i,j]
cell in the input matrix to the corresponding $[o,p]
cll in the output matrix. The value in
the input cell is multiplied by eq$fact
and all the values contributing to the value of cell
$[o,p]
in the output are added together to form the final result.
If a particular i,j
pair is not represented in the equivalence table then that pair will not contribute to the
output matrix. Indices that are beyond the dimensions of the input matrix may introduce NA
's into an output
cell, even if other correspondences introduce known values.
With suitable correspondence factors, matrices may be expanded or reduced in size.
A matrix
with dimensions [nrow,ncol]
computed by applying correspondence factors.
Jeremy Raw
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | # The basic mechanics of aggregating a matrix...
m <- matrix(1:100,nrow=10,ncol=10)
eq <- expand.grid(i=1:10,j=1:10)
eq$fact <- 1
eq$o <- ceiling(eq$i/2)
eq$p <- ceiling(eq$j/2)
om <- reaggregate.matrix(m,eq)
# Disaggregating is trickier since factors are necessary
req <- eq[,c("i","j","fact","o","p")]
names(req) <- c("o","p","fact","i","j")
req$fact <- 0.25
rom <- reaggregate.matrix(om,req)
# Building an equivalence table using a correspondence table
# Used in travel modeling to convert zones to districts
ct <- data.frame( from=c( 1, 2, 3, 4, 5, 6, 7, 8, 9,10),
to=c( 2, 2, 1, 3, 3, 3, 4, 4, 5, 5) )
c.eq <- expand.grid(i=1:10,j=1:10)
c.eq$o <- ct$to[match(c.eq$i,ct$from)]
c.eq$p <- ct$to[match(c.eq$j,ct$from)]
c.eq$fact <- 1
c.om <- reaggregate.matrix(m,c.eq)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.