Expand numeric vector elements with constant (natural number)
Given a vector v <- c(1, 10, 22)
and a constant natural number say c <- 3
, how can I expand v
with integers in the size window c
. So the vector will become w
(i.e. 1 expands three integers on each side, integers -2, -1, 0, 1, 2, 3, 4):
> w
[1] -2 -1 0 1 2 3 4 7 8 9 10 11 12 13 19 20 21 22 23 24 25
source to share
Another approach is
c(t(sapply(-c:c, `+`, v)))
#[1] -2 -1 0 1 2 3 4 7 8 9 10 11 12 13 19 20 21 22 23 24 25
And it is more efficient for large v vectors because the sapply loop only runs over -c:c
instead of each element v
. A simple comparison shows this:
set.seed(1)
v <- sample(1e6)
system.time(unlist( Map(`:`, v-c, v+c))) # akrun 1
# User System verstrichen
# 1.518 0.067 1.595
system.time(c(sapply(v, function(x) (x-c):(x+c)))) # akrun 2
# User System verstrichen
# 1.564 0.074 1.652
system.time(c(t(sapply(-c:c, '+', v)))) # docendo
# User System verstrichen
# 0.082 0.024 0.106
system.time(c(mapply(seq, v-c, v+c))) # 989
# User System verstrichen
# 7.132 0.123 7.292
source to share
Here's another very quick option (maybe not very elegant though ...):
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
source to share