centers: Find the middles of intervals

Description Usage Arguments Details Value Examples

View source: R/centers.R

Description

Find the middles of intervals as defined by the values of their boundaries. Makes more sense if the values of x are ordered, but doesn't have to.

Usage

1
centers(x, with_borders = FALSE)

Arguments

x

A vector of numeric values

with_borders

A logical value stating whether the middle values should only be in-between the first and last values of x (FALSE, default value) or be expanded outside this range (TRUE). See Details for more information.

Details

Basically a wrapper around the base R diff() function. In case with_borders = TRUE, the first and last values are found by reflection, see Examples.

Value

A vector of numeric values of shorter (if with_borders = FALSE) or longer (if with_borders = TRUE) by one element than the inputed x vector.

Examples

 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
# A simple example:
x <- 1:10
centers(x)
centers(x, TRUE)

# The effect of the option on the length of the output:
length(centers(x))
length(centers(x, TRUE))

# Works also if the input is not sorted (but makes less sense):
centers(sample(x))

# Let's consider another example to illustrate the effect of the option:
x <- c(1, 2, 4, 7, 11, 16, 22)
# where
diff(x)
# Now:
centers(x)
# These values are easy to understand. Let's now see:
centers(x, TRUE)
# Here is how the first (0.5) and last (25) values are computed. The first
# one is computed so that the first value of x (1) is in the middle of the
# interval defined by the first two values of centers(x, TRUE) (0.5 and 1.5).
# Similarly, the last value (25) is computed so that the last value of x (22)
# is in the middle of the interval defined by the last two values of
# centers(x, TRUE) (19 and 25).

choisy/mcstats documentation built on May 21, 2019, 3:08 a.m.