Title: | Column-Linked and Row-Linked Matrices |
---|---|
Description: | A class that links matrix-like objects (nodes) by rows or by columns while behaving similarly to a base R matrix. Very large matrices are supported if the nodes are file-backed matrices. |
Authors: | Gustavo de los Campos [aut], Alexander Grueneberg [aut, cre] |
Maintainer: | Alexander Grueneberg <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.4.0.9000 |
Built: | 2025-01-27 03:09:30 UTC |
Source: | https://github.com/quantgen/linkedmatrix |
A class that links matrix-like objects (nodes) by rows or by columns while behaving similarly to a base R matrix. Very large matrices are supported if the nodes are file-backed matrices.
ColumnLinkedMatrix-class
,
RowLinkedMatrix-class
, and LinkedMatrix-class
for more information on the ColumnLinkedMatrix
,
RowLinkedMatrix
, and LinkedMatrix
classes.
Converts an Object to a LinkedMatrix Object.
as.ColumnLinkedMatrix(x, ...) ## S3 method for class 'list' as.ColumnLinkedMatrix(x, ...) as.RowLinkedMatrix(x, ...) ## S3 method for class 'list' as.RowLinkedMatrix(x, ...)
as.ColumnLinkedMatrix(x, ...) ## S3 method for class 'list' as.ColumnLinkedMatrix(x, ...) as.RowLinkedMatrix(x, ...) ## S3 method for class 'list' as.RowLinkedMatrix(x, ...)
x |
An object to convert to a |
... |
Additional arguments. |
A LinkedMatrix
object.
ColumnLinkedMatrix-class
, RowLinkedMatrix-class
,
and LinkedMatrix-class
for more information on the
ColumnLinkedMatrix
, RowLinkedMatrix
, and LinkedMatrix
classes.
m1 <- ff::ff(initdata = rnorm(50), dim = c(5, 10)) m2 <- bigmemory::big.matrix(init = rnorm(50), nrow = 5, ncol = 10) m3 <- matrix(data = rnorm(50), nrow = 5, ncol = 10) myList <- list(m1, m2, m3) m <- as.ColumnLinkedMatrix(myList)
m1 <- ff::ff(initdata = rnorm(50), dim = c(5, 10)) m2 <- bigmemory::big.matrix(init = rnorm(50), nrow = 5, ncol = 10) m3 <- matrix(data = rnorm(50), nrow = 5, ncol = 10) myList <- list(m1, m2, m3) m <- as.ColumnLinkedMatrix(myList)
Converts a LinkedMatrix Instance to a Matrix (if Small Enough).
## S3 method for class 'LinkedMatrix' as.matrix(x, ...)
## S3 method for class 'LinkedMatrix' as.matrix(x, ...)
x |
Either a |
... |
Additional arguments (unused). |
A matrix.
ColumnLinkedMatrix-class
,
RowLinkedMatrix-class
, and LinkedMatrix-class
for more information on the ColumnLinkedMatrix
,
RowLinkedMatrix
, and LinkedMatrix
classes.
Compared to the ColumnLinkedMatrix
and RowLinkedMatrix
constructor functions, nested LinkedMatrix
objects that are passed
via ...
will not be treated as matrix-like objects, but their nodes
will be extracted and merged with the new ColumnLinkedMatrix
(for
cbind.ColumnLinkedMatrix
) or RowLinkedMatrix
(for
rbind.RowLinkedMatrix
) object for a more compact representation.
## S3 method for class 'ColumnLinkedMatrix' cbind(..., deparse.level = 0L) ## S3 method for class 'RowLinkedMatrix' rbind(..., deparse.level = 1L)
## S3 method for class 'ColumnLinkedMatrix' cbind(..., deparse.level = 0L) ## S3 method for class 'RowLinkedMatrix' rbind(..., deparse.level = 1L)
... |
Matrix-like objects to be combined by columns. |
deparse.level |
Currently unused, defaults to 0. |
cbind.ColumnLinkedMatrix
currently only works for
ColumnLinkedMatrix
objects, rbind.RowLinkedMatrix
only for
RowLinkedMatrix
.
ColumnLinkedMatrix-class
,
RowLinkedMatrix-class
, and LinkedMatrix-class
for more information on the ColumnLinkedMatrix
,
RowLinkedMatrix
, and LinkedMatrix
classes.
This function constructs a new ColumnLinkedMatrix
or
RowLinkedMatrix
object from a list of matrix-like objects.
ColumnLinkedMatrix(...) RowLinkedMatrix(...)
ColumnLinkedMatrix(...) RowLinkedMatrix(...)
... |
A sequence of matrix-like objects of the same row-dimension (for
|
A matrix-like object is one that has two dimensions and implements at least
dim
and [
. Each object needs to have the same number of rows
(for ColumnLinkedMatrix
) or columns (for RowLinkedMatrix
) to
be linked together. If no matrix-like objects are given, a single 1x1 node
of type matrix
filled with NA
is returned.
LinkedMatrix
objects can be nested as long as they are conformable.
Either a ColumnLinkedMatrix
or a RowLinkedMatrix
object.
LinkedMatrix
to create an empty, prespecified
LinkedMatrix
object.
# Create various matrix-like objects that correspond in dimensions m1 <- ff::ff(initdata = rnorm(50), dim = c(5, 10)) m2 <- bigmemory::big.matrix(init = rnorm(50), nrow = 5, ncol = 10) m3 <- matrix(data = rnorm(50), nrow = 5, ncol = 10) # Create a ColumnLinkedMatrix object cm <- ColumnLinkedMatrix(m1, m2, m3) # To specify the matrix-like objects as a list, use the `do.call` function rm <- do.call(RowLinkedMatrix, list(m1, m2, m3))
# Create various matrix-like objects that correspond in dimensions m1 <- ff::ff(initdata = rnorm(50), dim = c(5, 10)) m2 <- bigmemory::big.matrix(init = rnorm(50), nrow = 5, ncol = 10) m3 <- matrix(data = rnorm(50), nrow = 5, ncol = 10) # Create a ColumnLinkedMatrix object cm <- ColumnLinkedMatrix(m1, m2, m3) # To specify the matrix-like objects as a list, use the `do.call` function rm <- do.call(RowLinkedMatrix, list(m1, m2, m3))
This class treats a list of matrix-like objects that are linked together by
columns (ColumnLinkedMatrix
) or rows (RowLinkedMatrix
) and
have the same number of rows similarly to a regular matrix
by
implementing key methods such as [
and [<-
for extracting and
replacing matrix elements, dim
to retrieve dimensions, and
dimnames
and dimnames<-
to retrieve and set dimnames. Each
list element is called a node and can be extracted or replaced using
[[
and [[<-
. A matrix-like object is one that has two
dimensions and implements at least dim
and [
.
Internally, this class is an S4 class that contains list
. Each node
can be accessed using the [[
operator. lapply
is also
possible. ColumnLinkedMatrix
and RowLinkedMatrix
form a
class union called LinkedMatrix
.
[
[<-
dim
dimnames
dimnames<-
as.matrix
is.matrix
length
print
str
cbind
(for ColumnLinkedMatrix
)
rbind
(for RowLinkedMatrix
)
ColumnLinkedMatrix
and RowLinkedMatrix
to
create a ColumnLinkedMatrix
and RowLinkedMatrix
objects from
scratch. as.ColumnLinkedMatrix
and
as.RowLinkedMatrix
to create a ColumnLinkedMatrix
and
RowLinkedMatrix
objects from other objects.
LinkedMatrix
to create an empty, prespecified
LinkedMatrix
object. nNodes
to get the number of
nodes of a LinkedMatrix
object.
# Create various matrix-like objects that correspond in dimensions m1 <- ff::ff(initdata = rnorm(50), dim = c(5, 10)) m2 <- bigmemory::big.matrix(init = rnorm(50), nrow = 5, ncol = 10) m3 <- matrix(data = rnorm(50), nrow = 5, ncol = 10) # Link random matrices by columns cm <- ColumnLinkedMatrix(m1, m2, m3) dim(cm) # Link random matrices by rows rm <- RowLinkedMatrix(m1, m2, m3) dim(rm) # Get the number of nodes of each linked matrix nNodes(cm) nNodes(rm) # Extract specific rows of linked matrix cm[1, ] cm[1:3, ] rm[1, ] rm[1:3, ] # Extract specific columns of linked matrix cm[, 1] cm[, 1:3] rm[, 1] rm[, 1:3] # Extract specific rows and columns of linked matrix cm[1, 1] cm[1:3, 1:3] rm[1, 1] rm[1:3, 1:3] # Get a reference to one of the nodes n <- cm[[2]] class(n) == "big.matrix" # LinkedMatrix objects are matrix-like and can be nested rcm <- RowLinkedMatrix(cm, cm)
# Create various matrix-like objects that correspond in dimensions m1 <- ff::ff(initdata = rnorm(50), dim = c(5, 10)) m2 <- bigmemory::big.matrix(init = rnorm(50), nrow = 5, ncol = 10) m3 <- matrix(data = rnorm(50), nrow = 5, ncol = 10) # Link random matrices by columns cm <- ColumnLinkedMatrix(m1, m2, m3) dim(cm) # Link random matrices by rows rm <- RowLinkedMatrix(m1, m2, m3) dim(rm) # Get the number of nodes of each linked matrix nNodes(cm) nNodes(rm) # Extract specific rows of linked matrix cm[1, ] cm[1:3, ] rm[1, ] rm[1:3, ] # Extract specific columns of linked matrix cm[, 1] cm[, 1:3] rm[, 1] rm[, 1:3] # Extract specific rows and columns of linked matrix cm[1, 1] cm[1:3, 1:3] rm[1, 1] rm[1:3, 1:3] # Get a reference to one of the nodes n <- cm[[2]] class(n) == "big.matrix" # LinkedMatrix objects are matrix-like and can be nested rcm <- RowLinkedMatrix(cm, cm)
If j
for ColumnLinkedMatrix
or i
for
RowLinkedMatrix
is passed, it will only generate entries for the
given indices. sort
, which is set by default, determines whether
j
or i
should be sorted before building the index.
index(x, ...)
index(x, ...)
x |
Either a |
... |
Additional arguments (see Details). |
A matrix.
ColumnLinkedMatrix-class
,
RowLinkedMatrix-class
, and LinkedMatrix-class
for more information on the ColumnLinkedMatrix
,
RowLinkedMatrix
, and LinkedMatrix
classes.
This function creates an empty LinkedMatrix
object of a certain
size, a certain number of nodes, and certain types of nodes.
LinkedMatrix(nrow, ncol, nNodes, linkedBy, nodeInitializer, ...)
LinkedMatrix(nrow, ncol, nNodes, linkedBy, nodeInitializer, ...)
nrow |
The number of rows of the whole matrix. |
ncol |
The number of columns of the whole matrix. |
nNodes |
The number of nodes. |
linkedBy |
Whether the matrix is linked by |
nodeInitializer |
The name of a function or a function |
... |
Additional arguments passed into the |
A ColumnLinkedMatrix
object if linkedBy
is columns
or
a RowLinkedMatrix
object if linkedBy
is rows
.
ColumnLinkedMatrix
and RowLinkedMatrix
to
create ColumnLinkedMatrix
and RowLinkedMatrix
objects from a
list of matrix-like objects.
# Create an empty 15x10 RowLinkedMatrix with 3 matrix nodes m1 <- LinkedMatrix(nrow = 15, ncol = 10, nNodes = 3, linkedBy = "rows", nodeInitializer = "matrixNodeInitializer") dim(m1) nNodes(m1) all(sapply(m1, inherits, "matrix")) # Create an empty 15x10 RowLinkedMatrix with 3 ff nodes m2 <- LinkedMatrix(nrow = 15, ncol = 10, nNodes = 3, linkedBy = "rows", nodeInitializer = "ffNodeInitializer", vmode = "byte") dim(m2) nNodes(m2) all(sapply(m2, inherits, "ff_matrix")) # Create an empty 15x10 RowLinkedMatrix with 3 big.matrix nodes m3 <- LinkedMatrix(nrow = 15, ncol = 10, nNodes = 3, linkedBy = "rows", nodeInitializer = function(nodeIndex, nrow, ncol, ...) { bigmemory::big.matrix(nrow = nrow, ncol = ncol) }) dim(m3) nNodes(m3) all(sapply(m3, inherits, "big.matrix"))
# Create an empty 15x10 RowLinkedMatrix with 3 matrix nodes m1 <- LinkedMatrix(nrow = 15, ncol = 10, nNodes = 3, linkedBy = "rows", nodeInitializer = "matrixNodeInitializer") dim(m1) nNodes(m1) all(sapply(m1, inherits, "matrix")) # Create an empty 15x10 RowLinkedMatrix with 3 ff nodes m2 <- LinkedMatrix(nrow = 15, ncol = 10, nNodes = 3, linkedBy = "rows", nodeInitializer = "ffNodeInitializer", vmode = "byte") dim(m2) nNodes(m2) all(sapply(m2, inherits, "ff_matrix")) # Create an empty 15x10 RowLinkedMatrix with 3 big.matrix nodes m3 <- LinkedMatrix(nrow = 15, ncol = 10, nNodes = 3, linkedBy = "rows", nodeInitializer = function(nodeIndex, nrow, ncol, ...) { bigmemory::big.matrix(nrow = nrow, ncol = ncol) }) dim(m3) nNodes(m3) all(sapply(m3, inherits, "big.matrix"))
This class is abstract and no objects can be created from it. It can be
used to check whether an object is either of type ColumnLinkedMatrix
or of type RowLinkedMatrix
using is(x, "LinkedMatrix")
and to
assign methods for both ColumnLinkedMatrix
and
RowLinkedMatrix
classes, e.g. show
.
length
as.matrix
show
initialize
ColumnLinkedMatrix-class
and
RowLinkedMatrix-class
for implementations of column-linked
and row-linked matrices.
# Create an example RowLinkedMatrix from various matrix-like objects that # correspond in dimensions m <- RowLinkedMatrix( ff::ff(initdata = rnorm(50), dim = c(5, 10)), bigmemory::big.matrix(init = rnorm(50), nrow = 5, ncol = 10), matrix(data = rnorm(50), nrow = 5, ncol = 10) ) # Test if m is an object of either type ColumnLinkedMatrix or RowLinkedMatrix if (is(m, "LinkedMatrix")) { message("m is a LinkedMatrix") }
# Create an example RowLinkedMatrix from various matrix-like objects that # correspond in dimensions m <- RowLinkedMatrix( ff::ff(initdata = rnorm(50), dim = c(5, 10)), bigmemory::big.matrix(init = rnorm(50), nrow = 5, ncol = 10), matrix(data = rnorm(50), nrow = 5, ncol = 10) ) # Test if m is an object of either type ColumnLinkedMatrix or RowLinkedMatrix if (is(m, "LinkedMatrix")) { message("m is a LinkedMatrix") }
Returns the number of nodes.
nNodes(x)
nNodes(x)
x |
Either a |
The number of nodes.
ColumnLinkedMatrix-class
,
RowLinkedMatrix-class
, and LinkedMatrix-class
for more information on the ColumnLinkedMatrix
,
RowLinkedMatrix
, and LinkedMatrix
classes.
# Create an example RowLinkedMatrix from various matrix-like objects that # correspond in dimensions m <- RowLinkedMatrix( ff::ff(initdata = rnorm(50), dim = c(5, 10)), bigmemory::big.matrix(init = rnorm(50), nrow = 5, ncol = 10), matrix(data = rnorm(50), nrow = 5, ncol = 10) ) # Get the number of nodes of the RowLinkedMatrix nNodes(m)
# Create an example RowLinkedMatrix from various matrix-like objects that # correspond in dimensions m <- RowLinkedMatrix( ff::ff(initdata = rnorm(50), dim = c(5, 10)), bigmemory::big.matrix(init = rnorm(50), nrow = 5, ncol = 10), matrix(data = rnorm(50), nrow = 5, ncol = 10) ) # Get the number of nodes of the RowLinkedMatrix nNodes(m)
Returns the column or row indexes at which each node starts and ends.
nodes(x)
nodes(x)
x |
Either a |
A matrix.
ColumnLinkedMatrix-class
,
RowLinkedMatrix-class
, and LinkedMatrix-class
for more information on the ColumnLinkedMatrix
,
RowLinkedMatrix
, and LinkedMatrix
classes.