DataBackend for data.table which serves as an efficient in-memory data base.
Chapter in the mlr3book: https://mlr3book.mlr-org.com/technical.html#backends
Package mlr3db to interface out-of-memory data, e.g. SQL servers or duckdb.
mlr3::DataBackend
-> DataBackendLong
id_col
(character(1)
)
Name of the column containing the row ids.
value_col
(character()
)
Names of the columns containing the values.
date_col
(character(1)
)
Name of the column containing the timestamps.
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.
key_cols
(character()
)
Returns vector of all column names that are part of the primary key.
Inherited methods
new()
Creates a new instance of this R6 class.
Note that DataBackendLong
does not copy the input data, while as_data_backend()
calls data.table::copy()
.
as_data_backend()
also takes care about casting to a data.table()
and adds a primary key column if necessary.
DataBackendLong$new(data, primary_key, id_col, date_col)
data
(data.table::data.table()
)
The input data.table()
.
primary_key
(character(1)
| integer()
)
Name of the primary key column, or integer vector of row ids.
id_col
(character(1)
)
Name of the column containing the row ids.
date_col
(character(1)
)
Name of the column containing the timestamps.
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.
rows
integer()
Row indices.
cols
character()
Column names.
data_format
(character(1)
)
Desired data format, e.g. "data.table"
or "Matrix"
.
roll
(logical(1)
)
If TRUE
(default), rows are rolled to the nearest existing row id if necessary.
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.
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.
rows
integer()
Row indices.
cols
character()
Column names.
Total of missing values per column (named numeric()
).
data = data.table::data.table(id = 1:5, x = runif(5), y = sample(letters[1:3], 5, replace = TRUE))
b = mlr3::DataBackendDataTable$new(data, primary_key = "id")
print(b)
#> <DataBackendDataTable> (5x3)
#> id x y
#> 1 0.312678766 c
#> 2 0.569549315 c
#> 3 0.568735599 b
#> 4 0.401984156 c
#> 5 0.001417392 c
b$head(2)
#> id x y
#> 1: 1 0.3126788 c
#> 2: 2 0.5695493 c
b$data(rows = 1:2, cols = "x")
#> x
#> 1: 0.3126788
#> 2: 0.5695493
b$distinct(rows = b$rownames, "y")
#> $y
#> [1] "c" "b"
#>
b$missings(rows = b$rownames, cols = names(data))
#> id x y
#> 0 0 0