| Rle-class {S4Vectors} | R Documentation |
Rle objects
Description
The Rle class is a general container for storing an atomic vector
that is stored in a run-length 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 object
valuesand an integer or numeric vectorlengthswith all positive elements that represent how many times each value is repeated. The length of these two vectors must be the same.lengthscan be missing in which casevaluesis turned into an Rle.
Getters
In the code snippets below, x is an Rle object:
runLength(x):-
Returns the run lengths for
x. runValue(x):-
Returns the run values for
x. nrun(x):-
Returns the number of runs in
x. start(x):-
Returns the starts of the runs for
x. end(x):-
Returns the ends of the runs for
x. width(x):-
Same as
runLength(x).
Setters
In the code snippets below, x is an Rle object:
runLength(x) <- value:-
Replaces
xwith a new Rle object using run valuesrunValue(x)and run lengthsvalue. runValue(x) <- value:-
Replaces
xwith a new Rle object using run valuesvalueand 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 vector
from.
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 in
x. The vector will be coerced to the requestedmode, unlessmodeis "any", in which case the most appropriate type is chosen. as.factor(x),as(from, "factor"):Creates a factor object based on the values contained in
x.as.data.frame(x),as(from, "data.frame"):Creates a
data.framewith 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. Calling
decodeon a non-Rle will returnxby 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)]:-
Subsets
xby indexi, whereican be positive integers, negative integers, a logical vector of the same length asx, an Rle object of the same length asxcontaining logical values, or an IRanges object. Whendrop=FALSEreturns an Rle object. Whendrop=TRUE, returns an atomic vector. x[i] <- value:-
Replaces elements in
xspecified byiwith corresponding elements invalue. Supports the same types foriasx[i]. x %in% table:-
Returns a logical Rle representing set membership in
table. c(x, ..., ignore.mcols=FALSE):-
Concatenate Rle object
xand the Rle objects in...together. See?cin this package (the S4Vectors package) for more information about concatenating Vector derivatives. append(x, values, after = length(x)):-
Insert one Rle into another Rle.
valuesthe Rle to insert.
afterthe subscript in
xafter which the values are to be inserted.
findRun(x, vec):-
Returns an integer vector indicating the run indices in Rle
vecthat are referenced by the indices in the integer vectorx. head(x, n = 6L):-
If
nis non-negative, returns the first n elements ofx. Ifnis negative, returns all but the lastabs(n)elements ofx. is.na(x):-
Returns a logical Rle indicating which values are
NA. is.finite(x):-
Returns a logical Rle indicating which values are finite.
is.unsorted(x, na.rm = FALSE, strictly = FALSE):-
Returns a logical value specifying if
xis unsorted.na.rmremove missing values from check.
strictlycheck for _strictly_ increasing values.
length(x):-
Returns the underlying vector length of
x. match(x, table, nomatch = NA_integer_, incomparables = NULL):-
Matches the values in
xtotable:tablethe values to be matched against.
nomatchthe value to be returned in the case when no match is found.
incomparablesa vector of values that cannot be matched. Any value in
xmatching a value in this vector is assigned thenomatchvalue.
rep(x, times, length.out, each),rep.int(x, times):-
Repeats the values in
xthrough one of the following conventions:timesVector 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.outNon-negative integer. The desired length of the output vector.
eachNon-negative integer. Each element of
xis repeatedeachtimes.
rev(x):-
Reverses the order of the values in
x. show(object):-
Prints out the Rle object in a user-friendly way.
order(..., na.last=TRUE, decreasing=FALSE, method=c("auto", "shell", "radix")):-
Returns a permutation which rearranges its first argument into ascending or descending order, breaking ties by further arguments. See
order. sort(x, decreasing=FALSE, na.last=NA):-
Sorts the values in
x.decreasingIf
TRUE, sort values in decreasing order. IfFALSE, sort values in increasing order.na.lastIf
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 vector
subset. table(...):-
Returns a table containing the counts of the unique values. Supported arguments include
useNAwith values of ‘no’ and ‘ifany’. Multiple Rle's must be concatenated withc()before callingtable. tabulate(bin, nbins = max(bin, 1L, na.rm = TRUE)):-
Just like
tabulate, except optimized for Rle. tail(x, n = 6L):-
If
nis non-negative, returns the last n elements ofx. Ifnis negative, returns all but the firstabs(n)elements ofx. unique(x, incomparables = FALSE, ...):-
Returns the unique run values. The
incomparablesargument takes a vector of values that cannot be compared withFALSEbeing 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 vector-like object:
setdiff(x, y):Returns the unique elements in
xthat are not iny.union(x, y):-
Returns the unique elements in either
xory. intersect(x, y):-
Returns the unique elements in both
xandy.
Author(s)
P. Aboyoun
See Also
Rle-utils, Rle-runstat, and aggregate for more operations on Rle objects.
Examples
x <- Rle(10:1, 1:10)
x
runLength(x)
runValue(x)
nrun(x)
diff(x)
unique(x)
sort(x)
x[c(1,3,5,7,9)]
x > 4
x2 <- Rle(LETTERS[c(21:26, 25:26)], 8:1)
table(x2)
y <- Rle(c(TRUE,TRUE,FALSE,FALSE,TRUE,FALSE,TRUE,TRUE,TRUE))
y
as.vector(y)
rep(y, 10)
c(y, x > 5)