data.table vecseq. , vecseq(x, y, clamp) x y x: (x + len). vecseq(c(1L, 4L), c(2L, 6L), 5L) c(1L, 2L, 4L, 5L, 6L). clamp - , >= . , .
, vecseq, , . , , ( :)).
data <- c("ta", "tb", "tc", "tk", "tf")
require(data.table)
ff <- data.table:::vecseq
my_fun <- function(data) {
xmin = 2L
xmax = length(data)-1L
len = xmax-xmin+1L
tot = sum(xmax:xmin)
t1 = ff(rep(1L, len), xmax:xmin, tot)
t2 = rep.int(xmin:xmax, xmax:xmin)
idx = ff(t1,t2,sum(t2))
dt = data.table(x=data[idx], id=rep.int(seq_along(t2), t2))
setattr(dt, 'sorted', 'id')
dt[J(seq_along(t2)), list(list(x))]$V1
}
This seems pretty fast and matches the @flodel rating (excellent). When the data length reaches 250, the difference is about 0.2 seconds (this solution is faster). So there is not much difference.
source
share