DataBackend for data.table which serves as an efficient in-memory data base.
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
,
DataBackendMatrix
,
as_data_backend.Matrix()
Super class
mlr3::DataBackend
-> DataBackendDataTable
Public fields
compact_seq
logical(1)
IfTRUE
, row ids are a natural sequence from 1 tonrow(data)
(determined internally). In this case, row lookup uses faster positional indices instead of equi joins.
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.
Note that DataBackendDataTable
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.
Usage
DataBackendDataTable$new(data, primary_key)
Arguments
data
(
data.table::data.table()
)
The inputdata.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 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.
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
data = as.data.table(palmerpenguins::penguins)
data$id = seq_len(nrow(palmerpenguins::penguins))
b = DataBackendDataTable$new(data = data, primary_key = "id")
print(b)
#> <DataBackendDataTable> (344x9)
#> species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
#> <fctr> <fctr> <num> <num> <int> <int>
#> Adelie Torgersen 39.1 18.7 181 3750
#> Adelie Torgersen 39.5 17.4 186 3800
#> Adelie Torgersen 40.3 18.0 195 3250
#> Adelie Torgersen NA NA NA NA
#> Adelie Torgersen 36.7 19.3 193 3450
#> Adelie Torgersen 39.3 20.6 190 3650
#> sex year id
#> <fctr> <int> <int>
#> male 2007 1
#> female 2007 2
#> female 2007 3
#> <NA> 2007 4
#> female 2007 5
#> male 2007 6
#> [...] (338 rows omitted)
b$head()
#> Key: <id>
#> species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
#> <fctr> <fctr> <num> <num> <int> <int>
#> 1: Adelie Torgersen 39.1 18.7 181 3750
#> 2: Adelie Torgersen 39.5 17.4 186 3800
#> 3: Adelie Torgersen 40.3 18.0 195 3250
#> 4: Adelie Torgersen NA NA NA NA
#> 5: Adelie Torgersen 36.7 19.3 193 3450
#> 6: Adelie Torgersen 39.3 20.6 190 3650
#> sex year id
#> <fctr> <int> <int>
#> 1: male 2007 1
#> 2: female 2007 2
#> 3: female 2007 3
#> 4: <NA> 2007 4
#> 5: female 2007 5
#> 6: male 2007 6
b$data(rows = 100:101, cols = "species")
#> species
#> <fctr>
#> 1: Adelie
#> 2: Adelie
b$nrow
#> [1] 344
head(b$rownames)
#> [1] 1 2 3 4 5 6
b$ncol
#> [1] 9
b$colnames
#> [1] "species" "island" "bill_length_mm"
#> [4] "bill_depth_mm" "flipper_length_mm" "body_mass_g"
#> [7] "sex" "year" "id"
# alternative construction
as_data_backend(palmerpenguins::penguins)
#> <DataBackendDataTable> (344x9)
#> species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
#> <fctr> <fctr> <num> <num> <int> <int>
#> Adelie Torgersen 39.1 18.7 181 3750
#> Adelie Torgersen 39.5 17.4 186 3800
#> Adelie Torgersen 40.3 18.0 195 3250
#> Adelie Torgersen NA NA NA NA
#> Adelie Torgersen 36.7 19.3 193 3450
#> Adelie Torgersen 39.3 20.6 190 3650
#> sex year ..row_id
#> <fctr> <int> <int>
#> male 2007 1
#> female 2007 2
#> female 2007 3
#> <NA> 2007 4
#> female 2007 5
#> male 2007 6
#> [...] (338 rows omitted)