Skip to main content

9  Rows and columns

The examples use one raven:

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

r8filter() keeps the rows where a rule is true, and r8select() gives columns by name or by position:

r8filter(r, rule(sbp > 125 && sex == "f"))
<raven table> 1 row ⨉ 3 columns
      age     sbp  sex
   double  double  factor
1      61     155  f
r8select(r, c("sex", "age"))
<raven table> 3 rows ⨉ 2 columns
   sex        age
   factor  double
1  f           40
2  m           52
3  f           61

x[rule, columns] is the same in one call. Every index is R’s, as it is of a data frame, and a rule is an index of rows:

r[rule(age > 45), c("sex", "age")]
<raven table> 2 rows ⨉ 2 columns
   sex        age
   factor  double
1  m           52
2  f           61
r[2:3, "sex"]
<raven table> 2 rows ⨉ 1 column
   sex
   factor
1  m
2  f

r8sort() orders the rows by columns, the first foremost, and descending names those that descend. Missing cells come last, strings are in the order of their code points in every locale, and factors in that of their levels:

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

A name in rule() is a column, and val() is the value of an R expression. What a call means is in the call alone, whatever the table and the session hold:

older <- function(x, age) r8filter(x, rule(age > val(age)))
older(r, 55)
<raven table> 1 row ⨉ 3 columns
      age     sbp  sex
   double  double  factor
1      61     155  f

Without rule(), a name is R’s variable. Where R has none, the error writes the rule:

r[age > 45, ]
Error:
! R has no variable `age`, which is a column of the table; a condition on columns is a rule: rule(age > 45).

9.1 A rule is a value

A rule is declared on its own, filters every table that has the columns it names, and checks the rows of a type (Declaring a table type). !, &, and | make a rule of others:

adult <- rule(age >= 18)
high <- rule(sbp >= 140)
r8filter(r, adult & !high)
<raven table> 1 row ⨉ 3 columns
      age     sbp  sex
   double  double  factor
1      40     120  f

A rule is checked against the columns and their types before a row is read:

r8filter(r, rule(sex >= 2))
Error:
! The rule does not fit the table: `>=` compares two numbers, two dates, or two date-times, found a string and a number.

A string compared with a date or a date-time is one, rule(day >= "2020-01-31"). The cell of a factor is its label. Backticks quote a name that is not a name of R, rule(`Age (years)` > 40).

9.2 Missing cells

A row is kept where the rule is true. Where a cell is missing, a comparison is missing, so that the row is in neither part:

m <- Raven(a = c(1, NA, 3))
r8filter(m, rule(a > 1))
<raven table> 1 row ⨉ 1 column
        a
   double
1       3
r8filter(m, !rule(a > 1))
<raven table> 1 row ⨉ 1 column
        a
   double
1       1
r8filter(m, rule(is.na(a) || a > 1))
<raven table> 2 rows ⨉ 1 column
        a
   double
     1 NA
1      NA
2       3

NaN is missing too. A logical vector of R with a missing value is refused as an index.

9.3 Whole columns

mean(x), sum(x), min(x), and max(x) in a filter read the whole column, and are missing where one of its cells is. pmin(a, b) and pmax(a, b) are the least and the greatest cell of the row:

r8filter(r, rule(sbp > mean(sbp)))
<raven table> 2 rows ⨉ 3 columns
      age     sbp  sex
   double  double  factor
1      52     140  m
2      61     155  f

9.4 Package code

R CMD check takes a bare name in a function for a variable that nothing binds. A function of a package quotes the condition, which gives the same rule: rule(quote(age > val(cutoff))).

A position past the end and a column that the table does not have are errors:

r[4, ]
Error:
! The index asks for row 4; there are 3 rows.
r[, "agee"]
Error:
! `agee` is not a column; the nearest column is `age`.
© 2026 E.D. Gennatas