Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 7th, 2012  |  syntax: None  |  size: 0.56 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Split vector with overlapping samples in R
  2. vec = seq(1,1000)
  3. splitWithOverlap(vec, 200, 100)
  4.        
  5. [0:200] [100:300] [200:400] [300:500] ...
  6.        
  7. splitWithOverlap <- function(vec, seg.length, overlap) {
  8.   starts = seq(1, length(vec), by=seg.length-overlap)
  9.   ends   = starts + seg.length - 1
  10.   ends[ends > length(vec)] = length(vec)
  11.  
  12.   lapply(1:length(starts), function(i) vec[starts[i]:ends[i]])
  13. }
  14.        
  15. library(zoo)
  16. rollapply(1:1000, 200, by = 100, c)
  17.        
  18. co.intervals(vec, 9, 0.5)
  19.  # then use split, or shingle
  20.        
  21. shingle(vec, intervals=co.intervals(vec, 9, 0.5))