Math Operations
Use R’s built-in math functions for dosing, kinetics, and basic numerical work.
Arithmetic operators
R uses the same arithmetic operators you see in calculators and spreadsheets:
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ |
Add | 3 + 2 |
5 |
- |
Subtract | 10 - 4 |
6 |
* |
Multiply | 5 * 3 |
15 |
/ |
Divide | 20 / 4 |
5 |
^ |
Power | 2 ^ 3 |
8 |
%% |
Remainder after division | 53 %% 10 |
3 |
%/% |
Whole-number division | 53 %/% 10 |
5 |
The last two are especially useful when you need to split something into full units plus leftovers. x %/% y gives the number of complete groups, while x %% y gives what remains after those groups are removed.
Order of operations matters as soon as a formula has more than one step. Parentheses make the intended sequence explicit, especially in dosing calculations:
You can combine %% and %/% with the rest of the arithmetic operators when you need both a full-unit count and a remainder. In this example, we are asking two questions about a shipment of 53 tablets packed in boxes of 10:
- How many full boxes can we make?
- How many tablets are left after those full boxes are removed?
%/% answers the first question by counting only the complete groups of 10. %% answers the second question by returning the leftover tablets that do not fill another full box.
Powers, roots & logarithms
Use base R functions for common mathematical transformations:
sqrt()for square rootsexp()for \(e^x\)log()for natural logarithms by defaultlog10()for base-10 logslog2()for base-2 logs
log() returns the natural logarithm unless you set base = explicitly. In other words, log(x) means ln(x) in R.
First-order elimination is a classic use case:
Half-life follows directly from the same natural-log relationship:
Absolute value & sign
Use abs() to remove the sign from a value and sign() to keep only the direction.
A practical example is deviation from label claim:
abs() is useful when you care about the size of the error, while sign() is useful when you care about whether the result is above or below the target.
Trigonometric functions
R’s trigonometric functions are sin(), cos(), and tan().
R expects angles in radians, not degrees.
In this context, pi is the built-in constant for \(\pi\) and is essential for angle conversion. A full circle is \(2\pi\) radians, so \(180^\circ = \pi\) radians.
A simple helper converts degrees to radians:
For example, convert \(30^\circ\) before using sin():
You can also use pi directly for common reference angles:
That same conversion works for any angle you need in a formula or a geometry-style calculation.
Sequences
Use seq() to generate regular sequences of values. This is especially handy for time points, dose levels, or plotting grids.
The basic syntax is seq(from, to, by = step) when you want evenly spaced values. You can also use length.out when you care about how many values you want rather than the step size.
You can also create integer sequences with the : operator. For quick consecutive integers, : is shorter and often easier to read.
A regular sequence like this is a good starting point for plotting profiles or comparing changes over time.
Use : for simple integer steps (for example 1:10 or 10:1). Use seq() when you need a custom step size like 0.25, a fixed number of values (length.out), or more explicit control.
Differences & cumulative math
diff()
diff() is useful when you want to see how much a value changes from one time point to the next. In a dissolution experiment, for example, it can show the change in concentration between sampling times or the step-to-step change in volume after a withdrawal.
cumsum()
cumsum() adds values step by step so you can see the running total at each point. That makes it useful for tracking cumulative drug removed from samples in dissolution experiments, Caco-2 transport experiments, total dose delivered, or any other quantity that accumulates over time.
The same idea works for any running total. If each sample result is recorded separately, cumsum() gives the total amount removed up to each sampling point.
Other cumulative functions
These functions follow the same “running” pattern, but they track different summaries:
| Function | What it does |
|---|---|
cummax() |
Keeps the largest value seen so far |
cummin() |
Keeps the smallest value seen so far |
cumprod() |
Multiplies values cumulatively |
The na.rm trap: these cumulative functions do not have a general na.rm = TRUE option. If your input contains missing values, clean or handle them first before applying the calculation.