samedi 5 janvier 2019

How to substitute " for" loop and if/ else if statements? Extracting data form data frame

I am working in R on data frame consisting of time and precipitation data (over 3 000 000 values, measured every minute)

I would like to extract all "precipitation episodes", which are actually all moments when it was raining (dp != 0, but also including possible break in raining but no longer then "a" of the next measurements)

I would like all episodes to be stored in the new data frame with additional column with the number of each of them. First rain - 1, Second - 2....

The example of "precipitation episode":

time <- c("2013-01-01 11:39:00",
        "2013-01-01 11:40:00", "2013-01-01 11:41:00",
        "2013-01-01 11:42:00","2013-01-01 11:43:00",
        "2013-01-01 11:44:00","2013-01-01 11:45:00",
        "2013-01-01 11:46:00","2013-01-01 11:47:00",
        "2013-01-01 11:48:00","2013-01-01 11:49:00",
        "2013-01-01 11:50:00","2013-01-01 11:51:00",
        "2013-01-01 11:52:00","2013-01-01 11:53:00")

time <- as.POSIXct(time , origin="1899-12-30",tz="GMT")

p<- c(1.565, 1.565, 1.658, 1.795, 1.795, 1.795, 1.896, 1.896, 2.985, 2.985, 
      2.985, 2.985, 3.5, 3.7, 3.85)

df <- data.frame(time, p)   
dp <- diff(df$p)

df$dp<- c(dp,0)

I created a function using for loop and (a lot of) if conditions, which I hope represents well my intentions. Its currently not working well - still looking for the reason why.

rain.episodes<- function(x) {
  a<- 300
  episode.number <- 1
  rain <- reja.clean[1,] #just for column names

  for (i in 1:nrow(x)) {

    if (x[i,"dp"] >0) {
        rain[i,]<- x[i,]
        rain[i, "episode.number"]<- episode.number
        a<-0

    } else if (x[i,"dp"] ==0 & a<300) {
        rain[i,]<- x[i,]
        rain[i, "episode.number"]<- episode.number
        a<-a+1

    } else if (a==301) {
        episode.number<-episode.number+1

    } else{
          a<-a+1
    }
  }
  return(rain)
}

Is there any way to create a function helping me with that issue, with the same output as the one I pasted, but using different (better) method?

Aucun commentaire:

Enregistrer un commentaire