Skip to contents

A table type declares the columns of its values, which are tables: each holds columns, vectors of one length, each with a name. A value reads as a list of its columns, with $, [[, names(), and lapply(), and as a data frame with [i, j], dim(), head(), and as.data.frame(); lm() and the other functions that call as.data.frame() take one (Raven).

Usage

new_table_type(
  name,
  columns = list(),
  open = FALSE,
  description = "",
  version = 1L,
  rules = list()
)

Arguments

name

Character: The type name, an R8 name.

columns

Named list: The columns, each a field type, such as type_double(min = 0), or a column(), which takes a unit, a description, and whether the values are distinct. The cells of a column may be missing where its type is option().

open

Logical: If TRUE, values have other columns too.

description

Character: The description.

version

Integer: The version, which a migration increments.

rules

List of rule(): The rules that every row satisfies. A rule reads the cells of its row by the names of the columns.

Value

The type object, a function that constructs values: of a data frame, of a named list of vectors, of a value of another table, or of columns given by name.

Details

columns declares the columns that every value has. With open = TRUE, a value has other columns too, after the declared ones, of any name and of any of the column types. Raven is the open table type without declared columns. type_table() declares a table type without a name, for a field.

Copies

The columns of a data frame and of a list are not copied. The columns of a data.table are, since data.table changes a column in place. An operation on the columns shares them; one on the rows makes new columns. r8validate() checks a value again.

Operations

x[i, j] gives the rows i and the columns j, always as a table, and x[j] gives columns. i is a rule(), whose names are columns, x[rule(age > 40), ], or holds positions, all positive or all negative, or is a logical vector with one value per row; j holds names or positions. r8filter() and r8select() are functions for the two; r8sort() orders rows, and r8set() and the functions with it change columns. x$a <- value replaces a column, adds one to an open table, and, with NULL, removes one; the new column is checked, with the rules that read it. A result without a column that its type requires is a Raven. rbind() binds the rows of values of one type with the same columns and units.

as.matrix(), t(), is.na(), summary(), duplicated(), and other functions of R read a table as a data frame; unique(), split(), and na.omit() give tables. cbind(), transform(), within(), subset(), sort(), and aggregate() are errors that name the function of R8. merge() gives a data frame.

Examples

op <- options(r8.namespace = "demo")
Vitals <- new_table_type("Vitals", columns = list(
  age = column(type_double(min = 0), unit = "a"),
  sbp = type_double(min = 0, max = 400)
), open = TRUE)
v <- Vitals(data.frame(age = c(40, 52), sbp = c(120, 140), sex = c("f", "m")))
v
#> <demo::Vitals table> 2 rows ⨉ 3 columns
#>       age     sbp  sex
#>    double  double  character
#>       [a]
#> 1      40     120  f
#> 2      52     140  m
v$age
#> [1] 40 52
v[rule(age > 45), c("age", "sex")]
#> <raven table> 1 row ⨉ 2 columns
#>       age  sex
#>    double  character
#>       [a]
#> 1      52  m
try(v$sbp <- c(120, -1))
#> Error : Invalid `demo::Vitals` from `x$sbp <- value`: 1 problem
#> - `sbp[[2]]` must be at least 0; found -1. [below_min]
#> `sbp` is declared by `demo::Vitals`. A transformed column takes another name, or Raven(x) gives the columns without the type.
options(op)