Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CriInterval = function(x, criteria){
- names(x) = 1:length(x)
- i=1
- while(i<length(x)){
- j=1
- while(x[i+j] - x[i] < criteria & i+j <= length(x)){
- x[i+j]<-NA
- j=j+1
- }
- i=i+j
- }
- return(names(na.omit(x)))
- }
- CriInterval2 = function(x, criteria){
- i=1
- j=1
- while(!is.na(i[j]) & i[j]<length(x)){
- i = c(i, match(FALSE,x[i[j]:length(x)]-x[i[j]] < criteria) + i[j]-1)
- j=j+1
- }
- return(i)
- }
- TS = seq(ISOdatetime(2016,02,08,18,20,00),
- ISOdatetime(2017,02,08,18,20,00), "min")
- X=TS %>% sample(1e4) %>% sort %>% as.numeric()
- system.time(ci <- CriInterval(X,6*60*60))
- system.time(ci <- CriInterval2(X,6*60*60))
- match(FALSE,X-X[1]<6*60*60) %in% ci
- system.time(ci <- lapply(1:500,
- function(i) CriInterval(TS %>% sample(1e4) %>% sort %>% as.numeric(),
- criteria=6*60*60)))
- str(ci)
- # user system elapsed
- # 26.25 0.05 26.29
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement