Extract.rle: Indexing Methods for 'rle' Objects

Extract.rleR Documentation

Indexing Methods for rle Objects

Description

These methods provide indexing functionality for rle objects on the scale of the original scale (the elements of the vector that was compressed) where possible.

Usage

## S3 method for class 'rle'
x[i, ..., unclass = getOption("rle.unclass_index") %||% FALSE]

## S3 replacement method for class 'rle'
x[i, ..., unclass = getOption("rle.unclass_index") %||% FALSE] <- value

## S3 method for class 'rle'
x[[i, ..., unclass = getOption("rle.unclass_index") %||% FALSE]]

## S3 replacement method for class 'rle'
x[[i, ..., unclass = getOption("rle.unclass_index") %||% FALSE]] <- value

## S3 method for class 'rle'
x$name

## S3 replacement method for class 'rle'
x$name <- value

Arguments

x, i, name, value, ...

Arguments to indexing operators. See Extract documentation in the base package.

unclass

Logical: whether to process the arguments as if for an ordinary list; default other than FALSE can be set with options(rle.unclass_index=...).

Details

At this time, the rle following form of indexing are supported:

operation index effect
[ numeric >= 0 as vector
[ numeric < 0 no
[ logical no
[ character on rle
⁠[<-⁠ numeric >= 0 no
⁠[<-⁠ numeric < 0 no
⁠[<-⁠ logical no
⁠[<-⁠ character on rle
[[ numeric as vector
⁠[[<-⁠ numeric no
[[ character on rle
⁠[[<-⁠ character on rle
$ character on rle
⁠$<-⁠ character on rle

Generally, character indexes will access the underlying elements of the rle object, ⁠$lengths⁠ and ⁠$values⁠.

Value

For character indices, the corresponding sublists or elements of the rle object; for numeric indices, for [[ the element at the specified position and for [ an rle containing the elements at the specified position(s).

Note

Some of these methods and inputs produce an error in order to future-proof code that depends on the rle package by preventing their use.

See Also

index_to_run()

Examples


# Indexing by character or by $ works, including sub-indexing.
x <- rle(1:5)
x[["values"]] <- 2:6
x
x$values[2:3] <- 7:8
x

# From example(rle):
z <- c(TRUE, TRUE, FALSE, FALSE, TRUE, FALSE, TRUE, TRUE, TRUE)
rle(z)
rle(z)[3:5] # Extract a sub-rle
rle(z)[[4]] # Extract an element

stopifnot(identical(inverse.rle(rle(z)[3:5]), z[3:5]))
# Fractional:
stopifnot(identical(inverse.rle(rle(z)[3.5]), z[3.5]))
# Zero:
stopifnot(identical(inverse.rle(rle(z)[0]), z[0]))
# Out of range:
stopifnot(identical(inverse.rle(rle(z)[20]), z[20]))
# A mix:
strange <- c(20, 3:5, 0, NA, 1:2)
stopifnot(identical(inverse.rle(rle(z)[strange]), z[strange]))

statnet/rle documentation built on June 11, 2025, 8:30 p.m.