Miscellaneous functions
misc.RdThis page documents various functions that work for disords, and I will add to these from time to time as I add new functions that make sense for disord objects (or identify functions that break disord discipline).
Usage
# S4 method for class 'disord'
length(x)
# S4 method for class 'disord'
sort(x, decreasing = FALSE, ...)
# S4 method for class 'disord'
rev(x)
# S4 method for class 'disord'
sapply(X, FUN, ..., simplify = TRUE, USE.NAMES = TRUE)
# S4 method for class 'disord'
lapply(X, FUN, ...)
# S4 method for class 'disord,ANY'
match(x, table, nomatch = NA_integer_, incomparables = NULL)
# S4 method for class 'ANY,disord'
match(x, table, nomatch = NA_integer_, incomparables = NULL)
# S4 method for class 'disord,disord'
match(x, table, nomatch = NA_integer_, incomparables = NULL)
# S4 method for class 'disord'
unlist(x, recursive = TRUE)
# S4 method for class 'disord'
diff(x, ...)
# S4 method for class 'disord'
jitter(x, factor=1, amount=NULL)
binder(x,y)Arguments
- x, y, X
A
disordordisindexobject- table
Target table for matching
- nomatch
The value to be returned in the case when no match is found
- incomparables
A vector of values that cannot be matched
- FUN
The function to be applied
- decreasing
Logical indicating if the sort order should be decreasing
- recursive, simplify, USE.NAMES
Standard options passed to base functions
- factor, amount
Passed to
jitter()- ...
Further arguments passed to underlying methods
Details
Functions like sin() and abs() work as expected: they take
and return disord objects with the same hash as x (which
means that idiom like x + sin(x) is accepted). However, there
are a few functions that are a little more involved:
rev()reverses its argument and returns adisordobject with a reversed hash, which ensures thatrev(rev(x))==x(and the two are consistent).sort()returns a vector of sorted elements (not adisord)length()returns the length of the data component of the objectsapply(X,f)returns a disord object which is the result of applyingf()to each element ofX.match(x,table)should behave as expected but note that iftableis adisord, the result is not defined (because it is not known where the elements ofxoccur intable). Neverthelessx %in% tableis defined and returns adisordobject.lapply(x,f)returnsdisord(lapply(elements(x),f,...),h=hash(x)). Note that double square bracket extraction, as inx[[i]], is disallowed (seeextract.Rd).which()returns adisindobject when given a Booleandisordunlist()takes adisordlist, flattens it and returns adisordvector. It requires therecursiveflag ofbase::unlist()to beTRUE, which it is by default, interpreting this to mean “kill all the structure in any sublists”. If the list comprises only length-one vectors, the returned value retains the same hash as the argument; if not, a new hash is generated.diff()is undefined fordisordobjects.rbind()andcbind()are undefined fordisordobjects as they break disord discipline. Functionbinder()returns a generic, and hopefully informative, error message [the package defines methods forrbind2()andcbind2()]jitter()takes adisordobject, jitters the elements, and returns adisordobject with the correct hash code.
Note
Some functionality is not yet implemented. Factors, lists, and named
vectors do not behave entirely consistently in the package;
paste() gives inconsistent results when called with disords.
Also, for() loops are incompatible with disord discipline, as
they impose an ordering (for() accesses the .Data slot of
its argument, which is a regular R vector). Thus:
> (a <- disord(1:3))
A disord object with hash 555f6bea49e58a2c2541060a21c2d4f9078c3086 and elements
[1] 1 2 3
(in some order)
> for(i in a){print(i)}
[1] 1
[1] 2
[1] 3
>
Above, we see that for() uses the ordering of the .Data
slot of S4 object a, even though elements() has
not been explicitly called.
Examples
a <- disord(c(a=1,b=2,c=7))
a
#> A disord object with hash 25851c88b4618fcb86dcdc37259badbbd0186e6d and elements
#> [1] 1 2 7
#> (in some order)
names(a)
#> [1] "a" "b" "c"
length(a)
#> [1] 3
sqrt(a)
#> A disord object with hash 25851c88b4618fcb86dcdc37259badbbd0186e6d and elements
#> [1] 1.000000 1.414214 2.645751
#> (in some order)
# powers() and vars() in the mvp package return lists; see the vignette
# for more discussion.
l <- disord(list(3, 6:9, 1:10))
sapply(l, length)
#> A disord object with hash c052cb4aa9cd4011a681898987a65e44b9b81458 and elements
#> [1] 1 4 10
#> (in some order)
unlist(l)
#> A disord object with hash 8cfe47ac829367fb0012835a6521654ce4f1558e and elements
#> [1] 3 6 7 8 9 1 2 3 4 5 6 7 8 9 10
#> (in some order)
## Quick illustration of rev():
revstring <- function(s){paste(rev(unlist(strsplit(s, NULL))),collapse="")}
x <- rdis()
revstring(hash(x)) == hash(rev(x))
#> [1] TRUE