rle.utils {statnet.common} | R Documentation |
Simple utilities for operations on RLE-encoded vectors.
## S3 method for class 'rle' c(...) ## S3 method for class 'rle' !x binop.rle(e1, e2, FUN) ## S3 method for class 'rle' e1 | e2 ## S3 method for class 'rle' e1 & e2 compact.rle(x) ## S3 method for class 'rle' any(..., na.rm = FALSE) ## S3 method for class 'rle' all(..., na.rm = FALSE) ## S3 method for class 'rle' e1 * e2 ## S3 method for class 'rle' e1 / e2 ## S3 method for class 'rle' e1 - e2 ## S3 method for class 'rle' e1 + e2 ## S3 method for class 'rle' e1 ^ e2 ## S3 method for class 'rle' e1 %% e2 ## S3 method for class 'rle' e1 %/% e2 ## S3 method for class 'rle' e1 == e2 ## S3 method for class 'rle' e1 > e2 ## S3 method for class 'rle' e1 < e2 ## S3 method for class 'rle' e1 != e2 ## S3 method for class 'rle' e1 <= e2 ## S3 method for class 'rle' e1 >= e2 ## S3 method for class 'rle' rep(x, ..., scale = c("element", "run"))
... |
For |
x, e1, e2 |
Arguments to unary ( |
FUN |
A binary function or operator or a name of one. It is assumed to be vectorized: it expects two vectors of equal lengths and outputs a vector of the same length. |
na.rm |
|
scale |
whether to replicate the elements of the RLE-compressed vector or the runs. |
All functions return an rle()
object. By default, the
functions and the operators do not merge adjacent runs with
the same value. This must be done explicitly with compact.rle()
.
binop.rle
: Perform an arbitrary binary operation on the pair of vectors
represented by the rle()
objects.
compact.rle
: Compact the rle()
object by merging adjacent runs.
Since rle()
stores run lengths as integers, compact.rle()
will not merge runs that add up to lengths greater than what can
be represented by a 32-bit signed integer
(2147483647).
The rep()
method for rle()
objects is very limited at
this time: . Even though the default setting is to replicate
elements of the vector, only the run-replicating functionality is
implemented at this time.
x <- rle(as.logical(rbinom(10,1,.7))) y <- rle(as.logical(rbinom(10,1,.3))) stopifnot(c(inverse.rle(x),inverse.rle(y))==inverse.rle(c(x,y))) stopifnot((!inverse.rle(x))==inverse.rle(!x)) stopifnot((inverse.rle(x)|inverse.rle(y))==inverse.rle(x|y)) stopifnot((inverse.rle(x)&inverse.rle(y))==inverse.rle(x&y)) stopifnot(identical(rle(inverse.rle(x)&inverse.rle(y)),compact.rle(x&y))) big <- structure(list(lengths=as.integer(rep(.Machine$integer.max/4,6)), values=rep(TRUE,6)), class="rle") stopifnot(all(aggregate(as.numeric(lengths)~values, data=as.data.frame(unclass(big)),FUN=sum) == aggregate(as.numeric(lengths)~values, data=as.data.frame(unclass(compact.rle(big))), FUN=sum))) x <- rle(as.logical(rbinom(10,1,.9))) y <- rle(as.logical(rbinom(10,1,.1))) stopifnot(any(x)==any(inverse.rle(x))) stopifnot(any(y)==any(inverse.rle(y))) stopifnot(all(x)==all(inverse.rle(x))) stopifnot(all(y)==all(inverse.rle(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((inverse.rle(x)*inverse.rle(y))==inverse.rle(x*y)) stopifnot((inverse.rle(x)/inverse.rle(y))==inverse.rle(x/y)) stopifnot((-inverse.rle(y))==inverse.rle(-y)) stopifnot((inverse.rle(x)-inverse.rle(y))==inverse.rle(x-y)) stopifnot((+inverse.rle(y))==inverse.rle(+y)) stopifnot((inverse.rle(x)+inverse.rle(y))==inverse.rle(x+y)) stopifnot((inverse.rle(x)^inverse.rle(y))==inverse.rle(x^y)) stopifnot((inverse.rle(x)%%inverse.rle(y))==inverse.rle(x%%y)) stopifnot((inverse.rle(x)%/%inverse.rle(y))==inverse.rle(x%/%y)) stopifnot((inverse.rle(x)==inverse.rle(y))==inverse.rle(x==y)) stopifnot((inverse.rle(x)>inverse.rle(y))==inverse.rle(x>y)) stopifnot((inverse.rle(x)<inverse.rle(y))==inverse.rle(x<y)) stopifnot((inverse.rle(x)!=inverse.rle(y))==inverse.rle(x!=y)) stopifnot((inverse.rle(x)<=inverse.rle(y))==inverse.rle(x<=y)) stopifnot((inverse.rle(x)>=inverse.rle(y))==inverse.rle(x>=y)) x <- rle(sample(c(-1,+1), 10, c(.7,.3), replace=TRUE)) y <- rpois(length(x$lengths), 2) stopifnot(all(rep(inverse.rle(x), rep(y, x$lengths))==inverse.rle(rep(x, y, scale="run"))))