Skip to contents

An expression of the rule dialect as a value, as rule() is a condition: captured unevaluated, compiled against the columns of the table it is given with, and evaluated in Rust. A bare name is a column, val(expr) is the value of an R expression, and col(expr) is the column whose name an R expression gives. r8set() stores the column that an expression computes for every row, r8summarize() the value of an expression of aggregates for each group, and r8map() one expression over many columns, in which col() is each column in turn. field(option(T), compute = calc(expr)) declares a field that r8resolve() computes from the others.

Usage

calc(expr)

Arguments

expr

Expression: The expression, over the columns of a table.

Value

An r8_calc.

Results

A number is a double, except n(), count(), and a choice among integers (min(), max(), pmin(), pmax(), and if), which are integers; a logical, a string, a date, and a date-time keep their kinds, and the cell of a factor is its label. A missing operand gives a missing cell, and NA is a missing constant: if (d == 0) NA else n / d. An error in a row, such as a division by zero, is an r8_value_error that counts the rows. The unit of a result is the one the compiler infers: sum(), mean(), median(), min(), max(), +, -, abs(), round(), pmin(), pmax(), and if keep the common unit of their operands; *, /, ^, sqrt(), log(), exp(), n(), and count() give none.

Aggregates

n() is the number of rows, count(cond) the rows where cond is true, and sum(), mean(), median(), min(), and max() read whole columns: over all rows, or over the rows of each group with by. sum() is the exact sum, rounded once; mean() that sum over the count; median() the middle value, or the mean of the two middle ones. An aggregate over a missing cell is missing; na.rm = TRUE leaves the missing cells out. With no cell left, sum() is 0 and the others are missing.

Typed functions

A call of a name that is no function of the dialect calls a typed function of new_function(), found where calc() is called and matched to its parameters as R matches a call: calc(trimmed_mean(sbp, trim = 0.2)). The functions of the dialect come first, so a typed function named mean is not called by mean(). A parameter of vec(T) or of a factor takes the cells of its argument in the rows of each group, or of the table without by; a scalar parameter takes a constant or val(); an omitted parameter takes its constant default, and a default written in R is an error. The function returns one logical, integer, double, string, date, or date-time, or option() of one, and its value has no unit. Each call is checked as a direct call: a missing cell for a parameter of vec(T) is the call's error, with the group's keys, and a parameter of vec(option(T)) receives the missing cells. A function whose body is rust() is called from R8's Rust code through the body that its package registers, with the cells of each group as an R vector, and its body runs at most once for each call: a call whose arguments the checks refuse runs through the function's closure, which raises the error of a direct call, and an error or a refused result of the body is the error that a direct call raises. An R error or an interrupt inside the body continues as itself. A rule and a computed field call no function.

Examples

calc(weight_kg / height_m^2)
#> <r8 calc> weight_kg/height_m^2
calc(mean(sbp, na.rm = TRUE))
#> <r8 calc> mean(sbp, na.rm = TRUE)
m <- "auc"
calc(median(col(m)))
#> <r8 calc> median(auc)
calc(median(col()))
#> <r8 calc> median(col())
trimmed_mean <- new_function(
  "trimmed_mean",
  function(x = arg_vec(type_double()), trim = arg_double(default = 0)) {
    mean(x, trim = trim)
  },
  return_type = type_double()
)
d <- Raven(g = c("a", "b", "a", "b"), x = c(1, 2, 3, 40))
r8summarize(d, list(x = calc(trimmed_mean(x, trim = 0.25))), by = "g")
#> <raven table> 2 rows ⨉ 2 columns
#>    g               x
#>    character  double
#> 1  a               2
#> 2  b              21