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 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).
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.
Raven() takes a data frame, a tibble, a data.table, a named list of vectors, or columns by name:
<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:
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:
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]
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 |
<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
<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).
A column has a unit and a description, which print(), r8columns(), and the JSON document show:
<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
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.