Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

vec_arith

Arithmetic operations


Description

This generic provides a common double dispatch mechanism for all infix operators (+, -, /, *, ^, %%, %/%, !, &, |). It is used to power the default arithmetic and boolean operators for vctrs objects, overcoming the limitations of the base Ops generic.

Usage

vec_arith(op, x, y, ...)

## Default S3 method:
vec_arith(op, x, y, ...)

## S3 method for class 'logical'
vec_arith(op, x, y, ...)

## S3 method for class 'numeric'
vec_arith(op, x, y, ...)

vec_arith_base(op, x, y)

MISSING()

Arguments

op

An arithmetic operator as a string

x, y

A pair of vectors. For !, unary + and unary -, y will be a sentinel object of class MISSING, as created by MISSING().

...

These dots are for future extensions and must be empty.

Details

vec_arith_base() is provided as a convenience for writing methods. It recycles x and y to common length then calls the base operator with the underlying vec_data().

vec_arith() is also used in diff.vctrs_vctr() method via -.

See Also

stop_incompatible_op() for signalling that an arithmetic operation is not permitted/supported.

See vec_math() for the equivalent for the unary mathematical functions.

Examples

d <- as.Date("2018-01-01")
dt <- as.POSIXct("2018-01-02 12:00")
t <- as.difftime(12, unit = "hours")

vec_arith("-", dt, 1)
vec_arith("-", dt, t)
vec_arith("-", dt, d)

vec_arith("+", dt, 86400)
vec_arith("+", dt, t)
vec_arith("+", t, t)

vec_arith("/", t, t)
vec_arith("/", t, 2)

vec_arith("*", t, 2)

vctrs

Vector Helpers

v0.3.8
MIT + file LICENSE
Authors
Hadley Wickham [aut], Lionel Henry [aut, cre], Davis Vaughan [aut], data.table team [cph] (Radix sort based on data.table's forder() and their contribution to R's order()), RStudio [cph]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.