1

I have rows of data that are seconds apart, however I found some anomalies. The difference between some rows is 30min or above, so I want to split my data to multiple other data frames at that condition which means loop through my data frame and split when the difference in time is above 30min. I’ve tried this already but it splits my data to one row data frame.

 RBD < - function(x){
    i <- 0
    while(i < length(data$Time)){
        if(data$Time[i+1]-data$Time[i] > 60*30){
            rb <- 1

        }
        else{
            rb<-0
    }
     i <- i+1
    }
}


ListData <- Data %>%
    group_by(Data$temp)%>%
    transmute(ind=all((RBD = 1))%>%
    .$ind
names(ListData) <- paste0(‘Data’, seq_along(ListData))

split(Data, ListData)

My Data looks like this Data

Oumab10
  • 696
  • 2
  • 6
  • 14
  • To make it easier for others to help you, it is better to include a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610). – Jaap Mar 05 '18 at 08:50

1 Answers1

0

There's a very helpful function in base R: diff, which can do the heavy lifting for you. If this doesn't work for you, try posting a reprex and I'll see if i can help you troubleshoot.

Lets simulate some data:

set.seed(123)
x <- sample(1200, 100)
x <- x + sample(c(0, 0, 0, 0, 2400), 100, replace = TRUE)


RBD <- function(x){
    res <- lag(x) > 60*30
    res[1] <- FALSE
    res
}

RBD(x)
# [1]  FALSE FALSE FALSE FALSE  TRUE FALSE  TRUE  TRUE FALSE FALSE FALSE  TRUE   
# [13] FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE
# [25] FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE
# [37] FALSE  TRUE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE
# [49] FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
# [61] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
# [73] FALSE  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE
# [85] FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE FALSE  TRUE FALSE  TRUE
# [97] FALSE FALSE FALSE FALSE
De Novo
  • 7,120
  • 1
  • 23
  • 39
  • I did the lag function RBD <- funtion(x){ res <- Data$Time-lag(Data$Time) > 60*30 } It returned TRUE when facing these specific rows However when I try to split my data based on that ( function's result) ListData <- Data %>% group_by(Data$temp)%>% transmute(ind=all((RBD = TRUE))%>% .$ind It doesn't seem to work, do you have any suggestion – Oumab10 Mar 05 '18 at 10:18
  • I want to split my data each time it faces a TRUE ( I don't want to group the "TRUE" rows) – Oumab10 Mar 05 '18 at 10:23
  • Can you provide a [reproducible example](http://reprex.tidyverse.org/articles/reprex.html) ? – De Novo Mar 05 '18 at 10:31
  • I added an image of my data – Oumab10 Mar 05 '18 at 10:56