Data manipulation

library(basetable)

basetable uses compact verbs with explicit arguments. The same functions work in nested expressions and native R pipelines, always returning a the native engine without modifying the input.

A compact pipeline

mtcars |>
  subset(cyl >= 6, select = c("mpg", "hp", "wt", "cyl")) |>
  transform(power = hp / wt) |>
  orderrows(by = c("cyl", "mpg"), decreasing = c(FALSE, TRUE))
## # basetable: 21 x 5
##     mpg  hp    wt cyl    power
## 1  21.4 110 3.215   6 34.21462
## 2  21.0 110 2.620   6 41.98473
## 3  21.0 110 2.875   6 38.26087
## 4  19.7 175 2.770   6 63.17690
## 5  19.2 123 3.440   6 35.75581
## 6  18.1 105 3.460   6 30.34682
## 7  17.8 123 3.440   6 35.75581
## 8  19.2 175 3.845   8 45.51365
## 9  18.7 175 3.440   8 50.87209
## 10 17.3 180 3.730   8 48.25737
## # 11 more rows

orderrows() accepts one direction per column. This makes a mixed ascending/descending order explicit without a descending-expression mini-language.

orderrows(
  mtcars,
  by = c("cyl", "mpg"),
  decreasing = c(FALSE, TRUE)
) |>
  firstrows(6)
## # basetable: 6 x 11
##    mpg cyl  disp  hp drat    wt  qsec vs am gear carb
## 1 33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
## 2 32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
## 3 30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
## 4 30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
## 5 27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
## 6 26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2

Values from the calling function

Transformation expressions can combine table columns with ordinary values defined by the calling function. Newly created columns are available to later expressions in the same call.

scorecars <- function(data, horsepowerweight = 0.7) {
  data |>
    transform(
      weightedhp = hp * horsepowerweight,
      score = weightedhp / wt
    ) |>
    orderrows("score", decreasing = TRUE)
}

scorecars(mtcars) |>
  pick(c("mpg", "hp", "wt", "score")) |>
  firstrows(5)
## # basetable: 5 x 4
##    mpg  hp    wt    score
## 1 15.0 335 3.570 65.68627
## 2 15.8 264 3.170 58.29653
## 3 30.4 113 1.513 52.28024
## 4 14.3 245 3.570 48.03922
## 5 13.3 245 3.840 44.66146

Grouped summaries

aggregate(airquality, by = "Month", value = c("Ozone", "Temp"), fun = mean, na.rm = TRUE)
## # basetable: 5 x 3
##   Month    Ozone     Temp
## 1     5 23.61538 65.54839
## 2     6 29.44444 79.10000
## 3     7 59.11538 83.90323
## 4     8 59.96154 83.96774
## 5     9 31.44828 76.90000

The expression-oriented equivalent stays compact when several summaries use different functions.

summaries(
  airquality,
  ozone = mean(Ozone, na.rm = TRUE),
  temperature = mean(Temp, na.rm = TRUE),
  days = length(Temp),
  by = "Month"
)
## # basetable: 5 x 4
##   Month    ozone temperature days
## 1     5 23.61538    65.54839   31
## 2     6 29.44444    79.10000   30
## 3     7 59.11538    83.90323   31
## 4     8 59.96154    83.96774   31
## 5     9 31.44828    76.90000   30

Joins

merge(
  data.frame(id = 1:3, x = letters[1:3]),
  data.frame(id = c(2, 3, 4), y = LETTERS[2:4]),
  by = "id",
  all = TRUE
)
## # basetable: 4 x 3
##   id    x    y
## 1  1    a <NA>
## 2  2    b    B
## 3  3    c    C
## 4  4 <NA>    D

Validation in a pipeline

Assertions return the input invisibly when they pass, so a pipeline can fail close to the operation that violated its contract.

cars <- mtcars |>
  transform(car = rownames(mtcars)) |>
  firstcols("car")

assertcomplete(cars, c("car", "mpg", "cyl"))
assertunique(cars, "car")
assertrows(cars, mpg > 0)

Explicit package calls

A few compact names intentionally match base R or other table packages (subset(), merge(), transform(), split()). basetable does not ship dplyr-named verbs such as filter(), select(), or mutate(), so it can be attached alongside dplyr without shadowing its grammar. For the names it does share with other table packages, qualify the verb rather than changing the workflow grammar.

mtcars |>
  basetable::subset(cyl == 6) |>
  basetable::pick(c("mpg", "hp", "wt")) |>
  basetable::transform(power = hp / wt)
## # basetable: 7 x 4
##    mpg  hp    wt    power
## 1 21.0 110 2.620 41.98473
## 2 21.0 110 2.875 38.26087
## 3 21.4 110 3.215 34.21462
## 4 18.1 105 3.460 30.34682
## 5 19.2 123 3.440 35.75581
## 6 17.8 123 3.440 35.75581
## 7 19.7 175 2.770 63.17690