mapply:
c(1, unlist(mapply(function(s,e) tail(s:e,-1), head(c(1,x),-1), x)))
c(seq(x[1]-1),
unlist(sapply(seq(length(x)-1), function(i) head(x[i]:x[i+1], -1))),
tail(x,1))
(base R-)
library(microbenchmark)
set.seed(1)
x <- sample(1000, 500, replace = FALSE)
f_Frank <- function(x) Reduce(function(y, z) c(head(y,-1), tail(y,1):z), x, init=1L)
f_989_1 <- function(x) c(1, unlist(mapply(function(s,e) tail(s:e,-1), head(c(1,x),-1), x)))
f_989_2 <- function(x)
c(seq(x[1]-1),
unlist(sapply(seq(length(x)-1), function(i) head(x[i]:x[i+1], -1))),
tail(x,1))
f_akrun <- function(x){
v1 <- rle(unlist(Map(":", x[-length(x)], x[-1])))$values
c(seq(v1[1]), v1[-1])
}
r <- f_Frank(x)
all(r==f_989_1(x))
all(r==f_989_2(x))
all(r==f_akrun(x))
res <- microbenchmark(f_Frank(x), f_989_1(x), f_989_2(x), f_akrun(x))
print(res, order="mean")