File: pipe_arithmetic.Rmd

package info (click to toggle)
r-cran-doby 4.7.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,400 kB
  • sloc: makefile: 2
file content (65 lines) | stat: -rw-r--r-- 1,230 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
---
title: "Pipe-friendly Arithmetic Helpers in doBy"
author: "Søren Højsgaard"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Pipe-friendly Arithmetic Helpers in doBy}
  %\VignetteEngine{knitr::knitr}
  %\VignetteEncoding{UTF-8}
---

The **doBy** package includes a suite of simple, *pipe-friendly* arithmetic
helper functions that make common transformations clearer and more expressive
when using the native R pipe (`|>`).  

These helpers let you write transformations in readable pipelines
without nested or hard-to-read expressions. All functions are
vectorized and work with scalars or numeric vectors.

---

## Available functions

- `add()`: add a constant
- `subtract()`: subtract a constant
- `mult()`: multiply by a constant
- `divide()`: divide by a constant
- `reciprocal()`: compute 1/x
- `pow()`: raise to a power

---

## Load doBy

```r
library(doBy)
x <- c(1, 2, 3)

# Addition
x |> add(5)
# [1] 6 7 8

# Subtraction
x |> subtract(1)
# [1] 0 1 2

# Multiplication
x |> mult(10)
# [1] 10 20 30

# Division
x |> divide(2)
# [1] 0.5 1.0 1.5

# Reciprocal
x |> reciprocal()
# [1] 1.0000000 0.5000000 0.3333333

# Power
x |> pow(2)
# [1] 1 4 9
x |>
  mult(2) |>
  add(3) |>
  reciprocal()
```