DataBackend for Matrix
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.
mlr3::DataBackend
-> DataBackendMatrix
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.
new()
Creates a new instance of this R6 class.
DataBackendMatrix$new(data, dense = NULL, primary_key = NULL)
data
Matrix::Matrix()
The input Matrix::Matrix()
.
dense
data.frame()
.
Dense data, converted to data.table::data.table()
.
primary_key
(character(1)
| integer()
)
Name of the primary key column, or integer vector of row ids.
data()
Returns a slice of the data in the specified format.
Currently, the only supported formats are "data.table"
and "Matrix"
.
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.
DataBackendMatrix$data(rows, cols, data_format = "data.table")
rows
integer()
Row indices.
cols
character()
Column names.
data_format
(character(1)
)
Desired data format, e.g. "data.table"
or "Matrix"
.
head()
Retrieve the first n
rows.
DataBackendMatrix$head(n = 6L)
n
(integer(1)
)
Number of rows.
data.table::data.table()
of the first n
rows.
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.
DataBackendMatrix$distinct(rows, cols, na_rm = TRUE)
rows
integer()
Row indices.
cols
character()
Column names.
na_rm
logical(1)
Whether to remove NAs or not.
Named list()
of distinct values.
missings()
Returns the number of missing values per column in the specified slice of data. Non-existing rows and columns are silently ignored.
DataBackendMatrix$missings(rows, cols)
rows
integer()
Row indices.
cols
character()
Column names.
Total of missing values per column (named numeric()
).
Other DataBackend:
DataBackendDataTable
,
DataBackend
,
as_data_backend.Matrix()
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() b$data(1:3, b$colnames, data_format = "Matrix") b$data(1:3, b$colnames, data_format = "data.table")
Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.