Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split vector with overlapping samples in R

Tags:

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?

like image 284
kmera Avatar asked Jan 15 '12 18:01

kmera


People also ask

How to split vector and data frames into various groups in R?

However, merging and splitting is a common operation in any programming language, and today, we will see how to split vector and data frames into various groups in R. The split () is a built-in R function that divides the Vector or data frame into the groups defined by the function.

What is the difference between Split() and unsplit() in R?

The split () is a built-in R function that divides the Vector or data frame into the groups defined by the function. It accepts the vector or data frame as an argument and returns the data into groups. The unsplit () function in R does the reverse of the split () function.

What is the split function in R?

The split function allows dividing data in groups based on factor levels. In this tutorial we are going to show you how to split in R with different examples, reviewing all the arguments of the function. The split function divides the input data ( x) in different groups ( f ).

How to split a vector into two vectors in Python?

Hence, you can split a vector into two vectors where items are of the same group, passing the names of the vector with the names function to argument f. Let’s define a named vector using the c () function.


2 Answers

rollapply creates a matrix m whose rows are the desired vectors. Stop at that point omitting last line of code if such result is sufficient; otherwise, if a list of vectors is desired split it by row:

library(zoo)

x <- 1:1000  # test input

m <- rollapply(x, 200, by = 100, c)
split(m, row(m))
like image 58
G. Grothendieck Avatar answered Oct 23 '22 15:10

G. Grothendieck


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
like image 22
John Colby Avatar answered Oct 23 '22 14:10

John Colby