difference: Calculate the difference between two numbers or times

View source: R/rates_of_change.R

differenceR Documentation

Calculate the difference between two numbers or times

Description

Calculate a difference between two numbers or times. For numeric objects, the difference is calculated as x2 - x1. For time objects (i.e., DateTimeClasses or Dates), the difference is calculated using difftime. A function can be supplied to process outputs; e.g. abs, or numeric for difftime objects.

Usage

difference(x2, x1, f = NULL, ...)

Arguments

x2

A number, Dates or DateTimeClasses object.

x1

A number, Dates or DateTimeClasses object.

f

A function to process outputs.

...

Other arguments passed to difftime e.g. units = "secs".

Value

The function returns the difference between two numbers or times.

Author(s)

Edward Lavender

Examples


#### Example (1) Numeric difference
difference(1, 2)

#### Example (2) Numeric difference with post processing
difference(1, 2, f = abs)

#### Example (3) Difference with times
difference(as.POSIXct("2016-01-05"), as.POSIXct("2016-01-01"))

#### Example (4) Difference with times and units specified
difference(as.POSIXct("2016-01-05"), as.POSIXct("2016-01-01"), units = "mins")

#### Example (5) Difference with times, specified units and post-processing
difference(as.POSIXct("2016-01-05"), as.POSIXct("2016-01-01"), units = "mins", f = as.numeric)


edwardlavender/Tools4ETS documentation built on Nov. 29, 2022, 7:41 a.m.