Hier ist eine andere sehr schnelle Möglichkeit (vielleicht nicht sehr elegant aber ...):
w <- rep.int(v, rep(c*2+1,length(v))) + (-c:c)
Benchmark:
library(microbenchmark)
set.seed(1)
v <- sample(1e6)
c <- 3
microbenchmark(times=30,
docendo =c(t(sapply(-c:c, '+', v))),
digemall=rep.int(v, rep(c*2+1,length(v))) + (-c:c)
)
# Unit: milliseconds
# expr min lq mean median uq max neval
# docendo 81.04337 82.50133 100.7718 83.78972 99.89731 169.38202 30
# digemall 28.57355 30.28533 37.0091 31.01103 32.18491 90.90412 30
c <- 20
microbenchmark(times=30,
docendo =c(t(sapply(-c:c, '+', v))),
digemall=rep.int(v, rep(c*2+1,length(v))) + (-c:c)
)
# Unit: milliseconds
# expr min lq mean median uq max neval
# docendo 581.9529 626.4765 673.2964 663.0599 713.8367 787.1848 30
# digemall 174.3748 177.2943 198.9419 180.0702 200.0904 319.6669 30
'c (t (sapply (-3: 3, \ '+ \', v))) ' –