# Disordered indices with the `disordR` package: an introduction to class `disindex`

#### Robin Hankin

#### 2023-03-22

Experimental `S4` class `disindex` allows extraction methods, including list extraction, to operate with the output of `which()`. Consider the following R session:

    library("disordR")

    ## Loading required package: Matrix

    (d <- disord(c(4,6,1,2,3,4,5,1)))

    ## A disord object with hash 5be95946574d11fea8d1bf9089e3e2e56e5b2f4e and elements
    ## [1] 4 6 1 2 3 4 5 1
    ## (in some order)

    ind <- which(d>4)

Above, object `ind` points to those elements of `d` which exceed 4. Thus:

    d

    ## A disord object with hash 5be95946574d11fea8d1bf9089e3e2e56e5b2f4e and elements
    ## [1] 4 6 1 2 3 4 5 1
    ## (in some order)

    d[ind]

    ## A disord object with hash 41746e2d50f048e033e412d64bab0e2945f0dd28 and elements
    ## [1] 6 5
    ## (in some order)

    d[ind] <- 99
    d

    ## A disord object with hash 01dc141e3c9524510a3af3a676e0ccae071aecab and elements
    ## [1]  4 99  1  2  3  4 99  1
    ## (in some order)

However, we cannot assert that `ind` is elements 2 and 7 of `d`, for the elements of `d` are stored in an implementation-specific order. If we examine `ind` directly, we see:

    ind

    ## A disind object with hash 5be95946574d11fea8d1bf9089e3e2e56e5b2f4e and 2 (implementation-specific) elements

which correctly says that the elements of `ind` are implementation-specific. However, the main application of `disindex` objects is for list extraction.

    d <- disord(c(4,1,6,2))
    dl <- sapply(d,function(x){seq(from=5,to=x)})
    dl

    ## A disord object with hash 3064f3c0c65c071637a6b1c1691af992f0e47912 and elements
    ## [[1]]
    ## [1] 5 4
    ##
    ## [[2]]
    ## [1] 5 4 3 2 1
    ##
    ## [[3]]
    ## [1] 5 6
    ##
    ## [[4]]
    ## [1] 5 4 3 2
    ##
    ## (in some order)

Suppose I wish to extract from object `dl` just the element with the longest length. Noting that this would be a `disord`-compliant question, we would use:

    howlong <- unlist(lapply(dl,length))
    longest <- which(howlong == max(howlong))
    dl[[longest]]

    ## [1] 5 4 3 2 1