DataBackend for data.table which serves as an efficient in-memory data base.

See also

Super class

mlr3::DataBackend -> DataBackendDataTable

Public fields

compact_seq

logical(1)
If TRUE, row ids are a natural sequence from 1 to nrow(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

Public 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 input 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 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.

Usage

DataBackendDataTable$data(rows, cols, data_format = "data.table")

Arguments

rows

integer()
Row indices.

cols

character()
Column names.

data_format

(character(1))
Desired data format, e.g. "data.table" or "Matrix".


Method head()

Retrieve the first n rows.

Usage

DataBackendDataTable$head(n = 6L)

Arguments

n

(integer(1))
Number of rows.

Returns

data.table::data.table() of the first n rows.


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.

Usage

DataBackendDataTable$distinct(rows, cols, na_rm = TRUE)

Arguments

rows

integer()
Row indices.

cols

character()
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.

Usage

DataBackendDataTable$missings(rows, cols)

Arguments

rows

integer()
Row indices.

cols

character()
Column names.

Returns

Total of missing values per column (named numeric()).

Examples

data = as.data.table(iris) data$id = seq_len(nrow(iris)) b = DataBackendDataTable$new(data = data, primary_key = "id") print(b)
#> <DataBackendDataTable> (150x6) #> Sepal.Length Sepal.Width Petal.Length Petal.Width Species id #> 5.1 3.5 1.4 0.2 setosa 1 #> 4.9 3.0 1.4 0.2 setosa 2 #> 4.7 3.2 1.3 0.2 setosa 3 #> 4.6 3.1 1.5 0.2 setosa 4 #> 5.0 3.6 1.4 0.2 setosa 5 #> 5.4 3.9 1.7 0.4 setosa 6 #> [...] (144 rows omitted)
b$head()
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species id #> 1: 5.1 3.5 1.4 0.2 setosa 1 #> 2: 4.9 3.0 1.4 0.2 setosa 2 #> 3: 4.7 3.2 1.3 0.2 setosa 3 #> 4: 4.6 3.1 1.5 0.2 setosa 4 #> 5: 5.0 3.6 1.4 0.2 setosa 5 #> 6: 5.4 3.9 1.7 0.4 setosa 6
b$data(rows = 100:101, cols = "Species")
#> Species #> 1: versicolor #> 2: virginica
b$nrow
#> [1] 150
head(b$rownames)
#> [1] 1 2 3 4 5 6
b$ncol
#> [1] 6
b$colnames
#> [1] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width" "Species" #> [6] "id"
# alternative construction as_data_backend(iris)
#> <DataBackendDataTable> (150x6) #> Sepal.Length Sepal.Width Petal.Length Petal.Width Species ..row_id #> 5.1 3.5 1.4 0.2 setosa 1 #> 4.9 3.0 1.4 0.2 setosa 2 #> 4.7 3.2 1.3 0.2 setosa 3 #> 4.6 3.1 1.5 0.2 setosa 4 #> 5.0 3.6 1.4 0.2 setosa 5 #> 5.4 3.9 1.7 0.4 setosa 6 #> [...] (144 rows omitted)