ID EN
Miscellaneous

rle

R Base 3.6.2

Compute the lengths and values of runs of equal values in a vector -- or the reverse operation.

Syntax

R
rle(x)
inverse.rle(x, &#8230;)<p></p><p># S3 method for rle
print(x, digits = getOption("digits"), prefix = "", &#8230;)</p>

Arguments

Parameter Description
x a vector (atomic, not a list) for rle(); an object of class "rle" for inverse.rle().
&#8230; further arguments; ignored here.
digits number of significant digits for printing, see print.default.
prefix character string, prepended to each printed line.

Return Value

rle() returns an object of class "rle" which is a list with components: lengthsan integer vector containing the length of each run. valuesa vector of the same length as lengths with the corresponding values. inverse.rle() returns an atomic vector.

Details

‘vector’ is used in the sense of is.vector. Missing values are regarded as unequal to the previous value, even if that is also missing. inverse.rle() is the inverse function of rle(), reconstructing x from the runs.

Examples

Example
R
# NOT RUN {
x <- rev(rep(6:10, 1:5))
rle(x)
## lengths [1:5]  5 4 3 2 1
## values  [1:5] 10 9 8 7 6

z <- c(TRUE, TRUE, FALSE, FALSE, TRUE, FALSE, TRUE, TRUE, TRUE)
rle(z)
rle(as.character(z))
print(rle(z), prefix = "..| ")

N <- integer(0)
stopifnot(x == inverse.rle(rle(x)),
          identical(N, inverse.rle(rle(N))),
          z == inverse.rle(rle(z)))
# }