Skip to contents

r8summarize() gives one row for each group of the rows of x by the key columns by, or one row for the table without them: the keys, then the value of each expression of columns for the group. Each expression is a calc() that reads the cells of its columns within aggregates, evaluated in Rust: n(), count(), sum(), mean(), median(), min(), and max(), alone or in an expression, such as sum(w * x) / sum(w).

Usage

r8summarize(x, columns, by = NULL, n_threads = NULL)

Arguments

x

A value of a table.

columns

Named list of calc() values: The summaries, by the names of the result's columns.

by

Optional Character or numeric: The key columns, by name or by position.

n_threads

Optional Integer [1, Inf): The threads that the work in Rust may use; NULL takes getOption("r8.n_threads"), whose default is half the logical cores, at most 2 where R CMD check limits the cores, and at most OMP_THREAD_LIMIT. A result is the same for any number of threads.

Value

A Raven of the key columns and the summaries, with the units that the expressions infer.

Groups

A group is one combination of the values of the keys that a row has. Groups are in the order of r8sort() of the keys: ascending, missing last, strings by code point, and factors by level. A missing key is one group; a level of a factor without a row has no group. The key columns keep their kinds, levels, units, and descriptions.

Missing cells

An aggregate over a missing cell is missing, as in R; na.rm = TRUE in the aggregate leaves the missing cells out: mean(sbp, na.rm = TRUE).

Examples

v <- Raven(
  ward = c("ICU", "ICU", "Cardiology"),
  sbp = c(142, NA, 128)
)
r8summarize(
  v,
  list(n = calc(n()), sbp = calc(mean(sbp, na.rm = TRUE))),
  by = "ward"
)
#> <raven table> 2 rows ⨉ 3 columns
#>    ward              n     sbp
#>    character   integer  double
#> 1  Cardiology        1     128
#> 2  ICU               2     142