Compute a short expression for a given integer vector, typically an index, that can be expressed shortly, using : etc.

inv.seq(i)

Arguments

i

vector of (usually increasing) integers.

Value

a call (“the inside of an expression”) to be eval()ed to return the original i.

Author

Martin Maechler, October 1995; more elegant implementation from Tony Plate.

See also

rle for another kind of integer vector coding.

Examples

(rr <- inv.seq(i1 <- c(3:12, 20:24, 27, 30:33)))
#> c(3:12, 20:24, 27, 30:33)
eval(rr)
#>  [1]  3  4  5  6  7  8  9 10 11 12 20 21 22 23 24 27 30 31 32 33
stopifnot(eval(rr) == i1)

e2 <- expression(c(20:13, 3:12, -1:-4, 27, 30:31))
(i2 <- eval(e2))
#>  [1] 20 19 18 17 16 15 14 13  3  4  5  6  7  8  9 10 11 12 -1 -2 -3 -4 27 30 31
(r2 <- inv.seq(i2))
#> c(20:13, 3:12, -1:-4, 27, 30:31)
stopifnot(all.equal(r2, e2[[1]]))

## Had {mapply()} bug in this example:
ii <- c(1:3, 6:9, 11:16)
stopifnot(identical(ii, eval(inv.seq(ii))))