Skip to main content

8  Raven and tables

A table is a type whose values hold columns: vectors of one length, each with a name. A column has a type and, where it is declared, a range, and it may have a unit and a description. Rules hold across the columns of a row. R8 checks them in Rust, over R’s own vectors, and an error names the rule, the row, and the cells (Figure 8.1).

column type and range unit cells Vitals declared by new_table_type(); every value is checked id integer, distinct 1 2 3 age double [0, 130] a 40 52 61 sbp double [0, 400] mm[Hg] 120 118 155 dbp double [0, 300] mm[Hg] 80 130 95 admitted date 2026-01-04 2026-01-09 2026-02-02 A rule over a row order <- rule(dbp < sbp) evaluated by column, in Rust The problem, with its row and its cells dbp < sbp; found dbp = 130, sbp = 118, at dbp[[2]] A rule is a value. r8filter(x, order) gives the rows of any table where it is true, r8filter(x, !order) the rows where it is false, and saveRDS() keeps it.
Figure 8.1: A declared table, a rule over its rows, and a row that does not satisfy it.

There are two kinds of table. Raven takes any columns. new_table_type() declares a table type of one’s own, whose values are tables:

Raven A declared table
Columns any the declared ones, and others with open = TRUE
A column’s type that of its vector declared: a type, a range, allowed values, missing cells, distinct values
Units and descriptions given to a value declared, and given to the other columns of a value
Rules across columns applied to it, as filters declared: every row of every value satisfies them
A column that is replaced any vector checked, with the rules that read it
Use data as they come, and work on them data that have been checked, and stay so

Both read as lists of columns and as data frames, share R’s vectors, and have JSON documents and hashes. The chapters of this part are Rows and columns, Declaring a table type, Checking data, and Copies and large tables.

8.1 Raven

Raven() takes a data frame, a tibble, a data.table, a named list of vectors, or columns by name:

r <- Raven(
  age = c(40, 52, 61),
  sbp = c(120, 140, 155),
  sex = factor(c("f", "m", "f"))
)
r
<raven table> 3 rows ⨉ 3 columns
      age     sbp  sex
   double  double  factor
1      40     120  f
2      52     140  m
3      61     155  f

A value holds vectors, not a data frame. It reads as a list of its columns and as a data frame:

r$age
[1] 40 52 61
dim(r)
[1] 3 3
coef(lm(sbp ~ age, data = r))
(Intercept)         age 
  53.333333    1.666667 

lm(), ggplot2, and the other functions that call as.data.frame() take a table. r8to_data_frame(), which is as.data.frame() of a table, shares the columns, and is the way to functions that need a data frame, such as dplyr’s, for which implement(dplyr::filter, Type) declares a method.

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

A column is a logical, an integer, a double, a character vector, a factor, a Date, or a POSIXct, with missing cells. Its name is any text that does not begin with $. A column of another class is an error that names the conversion:

Raven(stay = as.difftime(c(3, 5), units = "days"))
Error:
! Invalid `R8::Raven`: 1 problem
- The value: column `stay` is a `difftime`; a column is a logical, an integer, a double, a character vector, a factor, a Date, or a POSIXct; as.numeric(x, units = "hours") gives numbers, whose unit r8annotate() records. [column_type]

8.2 Changes

A function that changes a table gives a new one, which shares the columns it keeps, and leaves its argument as it is:

Function Gives the table
r8set(x, list(name = vector)) with columns added or replaced
r8drop(x, columns) without columns
r8rename(x, from, to) with columns under other names
r8annotate(x, unit, description) with units and descriptions
rbind(x, y) with the rows of tables of one type
r <- r8set(r, list(bmi = c(22.1, 27.4, 24.8)))
r8rename(r, "sbp", "systolic")
<raven table> 3 rows ⨉ 4 columns
      age  systolic  sex        bmi
   double    double  factor  double
1      40       120  f         22.1
2      52       140  m         27.4
3      61       155  f         24.8
r8drop(r, c("sex", "bmi"))
<raven table> 3 rows ⨉ 2 columns
      age     sbp
   double  double
1      40     120
2      52     140
3      61     155

New columns are given in a list, so that a column may have any name, that of an argument included. r$bmi <- v is the short form for one column.

A column that is computed from others keeps nothing of them: no range, no rule, and no unit. A column of a raven that is replaced loses its unit and its description, and one of a declared table is checked (Declaring a table type).

8.3 Units and descriptions

A column has a unit and a description, which print(), r8columns(), and the JSON document show:

r <- r8annotate(
  r,
  unit = c(age = "a", sbp = "mm[Hg]"),
  description = c(age = "Age at admission")
)
r
<raven table> 3 rows ⨉ 4 columns
      age       sbp  sex        bmi
   double    double  factor  double
      [a]  [mm[Hg]]
1      40       120  f         22.1
2      52       140  m         27.4
3      61       155  f         24.8
r8columns(r)
  name   type   unit      description declared
1  age double      a Age at admission    FALSE
2  sbp double mm[Hg]             <NA>    FALSE
3  sex factor   <NA>             <NA>    FALSE
4  bmi double   <NA>             <NA>    FALSE

The hash and the equality of a table cover its columns in order, their types, units, and cells. They leave descriptions out.

© 2026 E.D. Gennatas