kmera - 1 year ago 81
R Question

# Split vector with overlapping samples in R

Say I have a vector with 1000 values and I want to 'split/partition/group' this vector into multiple vectors each with 200 values and an overlap of 100 values between them.

E.g.:

``````vec = seq(1,1000)
splitWithOverlap(vec, 200, 100)
``````

should return the following vectors:

``````[0:200] [100:300] [200:400] [300:500] ...
``````

... you get the idea.

Is there a function in R with which I can do that?

No, but this will do it:

``````splitWithOverlap <- function(vec, seg.length, overlap) {
starts = seq(1, length(vec), by=seg.length-overlap)
ends   = starts + seg.length - 1
ends[ends > length(vec)] = length(vec)

lapply(1:length(starts), function(i) vec[starts[i]:ends[i]])
}
``````
``````> splitWithOverlap(1:100, 20, 10)
[[1]]
[1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20

[[2]]
[1] 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

[[3]]
[1] 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

[[4]]
[1] 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

[[5]]
[1] 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

[[6]]
[1] 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

[[7]]
[1] 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

[[8]]
[1] 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

[[9]]
[1]  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100

[[10]]
[1]  91  92  93  94  95  96  97  98  99 100
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download