rle.utils {statnet.common}R Documentation

RLE utilities

Description

Simple utilities for operations on RLE-encoded vectors.

Usage

## 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"))

Arguments

...

For c, objects to be concatenated. The first object must be of class rle(). For rep, see documentation for rep().

x, e1, e2

Arguments to unary (x) and binary (e1 and e2) operators.

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

see documentation for any() and all().

scale

whether to replicate the elements of the RLE-compressed vector or the runs.

Value

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().

Functions

Note

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.

Examples


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"))))

[Package statnet.common version 4.0.0 Index]