Ops.rle {rle} | R Documentation |
Unary and Binary Operations for rle
Objects
Description
Unary and binary Arithmetic and Logic operators (with
exceptions given below) are implemented between two rle
objects
and between an rle
object and a scalar.
Usage
## S3 method for class 'rle'
Ops(e1, e2)
Arguments
e1 , e2 |
Arguments to unary ( |
Details
Supported operations include all elements of the Ops
group, as well as xor
. Within the Arithmetic and Logic
operators, this includes (taken from the R help): +
, -
, *
,
/
, ^
, <
, >
, <=
, >=
, !=
, ==
, %%
, %/%
, &
,
|
, !
, and xor
; but excludes non-vector logical functions
and operators such as isTRUE
and &&
.
Value
In every supported case, the operation should result in an
rle
that would have resulted had the operation been applied
to the original (uncompressed) vectors, then compressed using
rle
, with the proviso that if the resulting function creates
adjacent runs of the same value, they are not merged. This must
be done explicitly with compress.rle
. (At no point in the
calculation are the uncompressed vectors actually constructed, of
course.)
An operation between an rle
and a zero-length object produces
an empty rle
.
Examples
x <- rle(as.logical(rbinom(10,1,.7)))
y <- rle(as.logical(rbinom(10,1,.3)))
stopifnot(isTRUE(all.equal((!inverse.rle(x)),inverse.rle(!x))))
stopifnot(isTRUE(all.equal((inverse.rle(x)|inverse.rle(y)),inverse.rle(x|y))))
stopifnot(isTRUE(all.equal((inverse.rle(x)&inverse.rle(y)),inverse.rle(x&y))))
x <- rle(sample(c(-1,+1), 10, c(.7,.3), replace=TRUE))
y <- rle(sample(c(-1,+1), 10, c(.3,.7), replace=TRUE))
stopifnot(isTRUE(all.equal((inverse.rle(x)*inverse.rle(y)),inverse.rle(x*y))))
stopifnot(isTRUE(all.equal((2*inverse.rle(y)),inverse.rle(2*y))))
stopifnot(isTRUE(all.equal((inverse.rle(x)*2),inverse.rle(x*2))))
stopifnot(isTRUE(all.equal((inverse.rle(x)/inverse.rle(y)),inverse.rle(x/y))))
stopifnot(isTRUE(all.equal((2/inverse.rle(y)),inverse.rle(2/y))))
stopifnot(isTRUE(all.equal((inverse.rle(x)/2),inverse.rle(x/2))))
stopifnot(isTRUE(all.equal((-inverse.rle(y)),inverse.rle(-y))))
stopifnot(isTRUE(all.equal((inverse.rle(x)-inverse.rle(y)),inverse.rle(x-y))))
stopifnot(isTRUE(all.equal((inverse.rle(x)%/%inverse.rle(y)),inverse.rle(x%/%y))))
stopifnot(isTRUE(all.equal(inverse.rle(x)==inverse.rle(y),inverse.rle(x==y))))
stopifnot(isTRUE(all.equal((inverse.rle(x)>inverse.rle(y)),inverse.rle(x>y))))