Advertisement
Edster

downsacle data by criteria of time interval

Feb 9th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 0.90 KB | None | 0 0
  1. CriInterval = function(x, criteria){
  2.   names(x) = 1:length(x)
  3.   i=1
  4.   while(i<length(x)){
  5.     j=1
  6.     while(x[i+j] - x[i] < criteria & i+j <= length(x)){
  7.       x[i+j]<-NA
  8.       j=j+1
  9.     }
  10.      i=i+j
  11.   }
  12.   return(names(na.omit(x)))
  13. }
  14.  
  15. CriInterval2 = function(x, criteria){
  16.   i=1
  17.   j=1
  18.   while(!is.na(i[j]) & i[j]<length(x)){
  19.     i = c(i, match(FALSE,x[i[j]:length(x)]-x[i[j]] < criteria) + i[j]-1)
  20.     j=j+1
  21.   }
  22.   return(i)
  23. }
  24.  
  25. TS = seq(ISOdatetime(2016,02,08,18,20,00),
  26.          ISOdatetime(2017,02,08,18,20,00), "min")
  27. X=TS %>% sample(1e4) %>% sort %>% as.numeric()
  28.  
  29. system.time(ci <- CriInterval(X,6*60*60))
  30. system.time(ci <- CriInterval2(X,6*60*60))
  31. match(FALSE,X-X[1]<6*60*60) %in% ci
  32.  
  33. system.time(ci <- lapply(1:500,
  34.        function(i) CriInterval(TS %>% sample(1e4) %>% sort %>% as.numeric(),
  35.        criteria=6*60*60)))
  36. str(ci)
  37.  
  38. #   user  system elapsed
  39. #  26.25    0.05   26.29
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement