DataBackend for Matrix.
Data is split into a (numerical) sparse part and an optional dense part.
These parts are automatically merged to a sparse format during $data()
.
Note that merging both parts potentially comes with a data loss, as all
dense columns are converted to numeric columns.
See also
Chapter in the mlr3book: https://mlr3book.mlr-org.com/chapters/chapter10/advanced_technical_aspects_of_mlr3.html#sec-backends
Package mlr3db to interface out-of-memory data, e.g. SQL servers or duckdb.
Other DataBackend:
DataBackend
,
DataBackendDataTable
,
as_data_backend.Matrix()
Super class
mlr3::DataBackend
-> DataBackendMatrix
Active bindings
rownames
(
integer()
)
Returns vector of all distinct row identifiers, i.e. the contents of the primary key column.colnames
(
character()
)
Returns vector of all column names, including the primary key column.nrow
(
integer(1)
)
Number of rows (observations).ncol
(
integer(1)
)
Number of columns (variables), including the primary key column.
Methods
Inherited methods
Method new()
Creates a new instance of this R6 class.
Usage
DataBackendMatrix$new(data, dense, primary_key = NULL)
Arguments
data
Matrix::Matrix()
The inputMatrix::Matrix()
.dense
data.frame()
. Dense data, converted todata.table::data.table()
.primary_key
(
character(1)
|integer()
)
Name of the primary key column, or integer vector of row ids.
Method data()
Returns a slice of the data as "data.table"
.
The rows must be addressed as vector of primary key values, columns must be referred to via column names.
Queries for rows with no matching row id and queries for columns with no matching column name are silently ignored.
Rows are guaranteed to be returned in the same order as rows
, columns may be returned in an arbitrary order.
Duplicated row ids result in duplicated rows, duplicated column names lead to an exception.
Arguments
rows
(positive
integer()
)
Vector or row indices. Always refers to the complete data set, even after filtering.cols
(
character()
)
Vector of column names.data_format
(
character(1)
)
Deprecated. Ignored, and will be removed in the future.
Method distinct()
Returns a named list of vectors of distinct values for each column
specified. If na_rm
is TRUE
, missing values are removed from the
returned vectors of distinct values. Non-existing rows and columns are
silently ignored.
Arguments
rows
(positive
integer()
)
Vector or row indices. Always refers to the complete data set, even after filtering.cols
(
character()
)
Vector of column names.na_rm
logical(1)
Whether to remove NAs or not.
Returns
Named list()
of distinct values.
Method missings()
Returns the number of missing values per column in the specified slice of data. Non-existing rows and columns are silently ignored.
Arguments
rows
(positive
integer()
)
Vector or row indices. Always refers to the complete data set, even after filtering.cols
(
character()
)
Vector of column names.
Returns
Total of missing values per column (named numeric()
).
Examples
requireNamespace("Matrix")
data = Matrix::Matrix(sample(0:1, 20, replace = TRUE), ncol = 2)
colnames(data) = c("x1", "x2")
dense = data.frame(
..row_id = 1:10,
num = runif(10),
fact = factor(sample(c("a", "b"), 10, replace = TRUE), levels = c("a", "b"))
)
b = as_data_backend(data, dense = dense, primary_key = "..row_id")
b$head()
#> ..row_id num fact x1 x2
#> <int> <num> <fctr> <num> <num>
#> 1: 1 0.57207372 b 0 1
#> 2: 2 0.70381295 a 1 1
#> 3: 3 0.65722106 b 0 0
#> 4: 4 0.28935215 b 1 1
#> 5: 5 0.09723946 a 1 0
#> 6: 6 0.96242132 a 0 1
b$data(1:3, b$colnames)
#> ..row_id num fact x1 x2
#> <int> <num> <fctr> <num> <num>
#> 1: 1 0.5720737 b 0 1
#> 2: 2 0.7038130 a 1 1
#> 3: 3 0.6572211 b 0 0