Rle objects
Description
The Rle class is a general container for storing an atomic vector
that is stored in a runlength encoding format. It is based on the
rle
function from the base package.
Constructor

Rle(values, lengths)
: This constructor creates an Rle instance out of an atomic vector or factor objectvalues
and an integer or numeric vectorlengths
with all positive elements that represent how many times each value is repeated. The length of these two vectors must be the same.lengths
can be missing in which casevalues
is turned into an Rle.
Getters
In the code snippets below, x
is an Rle object:

runLength(x)
: Returns the run lengths forx
. 
runValue(x)
: Returns the run values forx
. 
nrun(x)
: Returns the number of runs inx
. 
start(x)
: Returns the starts of the runs forx
. 
end(x)
: Returns the ends of the runs forx
. 
width(x)
: Same asrunLength(x)
.
Setters
In the code snippets below, x
is an Rle object:

runLength(x) < value
: Replacesx
with a new Rle object using run valuesrunValue(x)
and run lengthsvalue
. 
runValue(x) < value
: Replacesx
with a new Rle object using run valuesvalue
and run lengthsrunLength(x)
.
Coercion
From atomic vector to Rle
In the code snippets below, from
is an atomic vector:

as(from, "Rle")
: This coercion creates an Rle instances out of an atomic vectorfrom
.
From Rle to other objects
In the code snippets below, x
and from
are Rle objects:

as.vector(x, mode="any")
,as(from, "vector")
: Creates an atomic vector based on the values contained inx
. The vector will be coerced to the requestedmode
, unlessmode
is "any", in which case the most appropriate type is chosen. 
as.vectorORfactor(x)
: Creates an atomic vector or factor, based on the type of values contained inx
. This is the most general way to decompress the Rle to a native R data structure. 
as.factor(x)
,as(from, "factor")
: Creates a factor object based on the values contained inx
. 
as.data.frame(x)
,as(from, "data.frame")
: Creates adata.frame
with a single column holding the result ofas.vector(x)
. 
decode(x)
: Converts an Rle to its native form, such as an atomic vector or factor. Callingdecode
on a nonRle will returnx
by default, so it is generally safe for ensuring that an object is native.
General Methods
In the code snippets below, x
is an Rle object:

x[i, drop=getOption("dropRle", default=FALSE)]
: Subsetsx
by indexi
, wherei
can be positive integers, negative integers, a logical vector of the same length asx
, an Rle object of the same length asx
containing logical values, or an IRanges object. Whendrop=FALSE
returns an Rle object. Whendrop=TRUE
, returns an atomic vector. 
x[i] < value
: Replaces elements inx
specified byi
with corresponding elements invalue
. Supports the same types fori
asx[i]
. 
x %in% table
: Returns a logical Rle representing set membership intable
. 
append(x, values, after = length(x))
: Insert one Rle into another Rle.values
the Rle to insert.
after
the subscript in
x
after which the values are to be inserted.

c(x, ...)
: Combines a set of Rle objects. 
findRun(x, vec)
: Returns an integer vector indicating the run indices in Rlevec
that are referenced by the indices in the integer vectorx
. 
head(x, n = 6L)
: Ifn
is nonnegative, returns the first n elements ofx
. Ifn
is negative, returns all but the lastabs(n)
elements ofx
. 
is.na(x)
: Returns a logical Rle indicating with values areNA
. 
is.unsorted(x, na.rm = FALSE, strictly = FALSE)
: Returns a logical value specifying ifx
is unsorted.na.rm
remove missing values from check.
strictly
check for _strictly_ increasing values.

length(x)
: Returns the underlying vector length ofx
. 
match(x, table, nomatch = NA_integer_, incomparables = NULL)
: Matches the values inx
totable
:table
the values to be matched against.
nomatch
the value to be returned in the case when no match is found.
incomparables
a vector of values that cannot be matched. Any value in
x
matching a value in this vector is assigned thenomatch
value.

rep(x, times, length.out, each)
,rep.int(x, times)
: Repeats the values inx
through one of the following conventions:times
Vector giving the number of times to repeat each element if of length
length(x)
, or to repeat the whole vector if of length 1.length.out
Nonnegative integer. The desired length of the output vector.
each
Nonnegative integer. Each element of
x
is repeatedeach
times.

rev(x)
: Reverses the order of the values inx
. 
show(object)
: Prints out the Rle object in a userfriendly way. 
order(..., na.last = TRUE, decreasing = FALSE, method = c("shell", "radix"))
: Returns a permutation which rearranges its first argument into ascending or descending order, breaking ties by further arguments. Seeorder
. 
sort(x, decreasing = FALSE, na.last = NA)
: Sorts the values inx
.decreasing
If
TRUE
, sort values in decreasing order. IfFALSE
, sort values in increasing order.na.last
If
TRUE
, missing values are placed last. IfFALSE
, they are placed first. IfNA
, they are removed.

subset(x, subset)
: Returns a new Rle object made of the subset using logical vectorsubset
. 
table(...)
: Returns a table containing the counts of the unique values. Supported arguments includeuseNA
with values of ‘no’ and ‘ifany’. Multiple Rle's must be combined withc()
before callingtable
. 
tabulate(bin, nbins = max(bin, 1L, na.rm = TRUE))
: Just liketabulate
, except optimized for Rle. 
tail(x, n = 6L)
: Ifn
is nonnegative, returns the last n elements ofx
. Ifn
is negative, returns all but the firstabs(n)
elements ofx
. 
unique(x, incomparables = FALSE, ...)
: Returns the unique run values. Theincomparables
argument takes a vector of values that cannot be compared withFALSE
being a special value that means that all values can be compared.
Set Operations
In the code snippets below, x
and y
are Rle object or
some other vectorlike object:

setdiff(x, y)
: Returns the unique elements inx
that are not iny
. 
union(x, y)
: Returns the unique elements in eitherx
ory
. 
intersect(x, y)
: Returns the unique elements in bothx
andy
.
Author(s)
P. Aboyoun
See Also
Rleutils, Rlerunstat, and aggregate for more operations on Rle objects.
rle
Vectorclass
Examples
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 
Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker. Vote for new features on Trello.