Search code examples
rrowsclosest

How to find the first smaller value compared to the current row in subsequent rows?


Suppose this is the data:

data<-data.frame(number=c(4,5,3,1,0), 
             datetime=c(as.POSIXct("2015/06/12 12:10:25"),
                        as.POSIXct("2015/06/12 12:10:27"),
                        as.POSIXct("2015/06/12 12:10:32"),
                        as.POSIXct("2015/06/12 12:10:33"),
                        as.POSIXct("2015/06/12 12:10:35")))

  number   datetime
1      4 2015/06/12 12:10:25
2      5 2015/06/12 12:10:27
3      3 2015/06/12 12:10:32
4      1 2015/06/12 12:10:33
5      0 2015/06/12 12:10:35

I want to calculate the time between a row to the next smaller value. Desired output:

   number  next smaller   time between
1      4              3             7
2      5              3             5
3      3              1             1
4      1              0             2
5      0             NA          NA

Example: 3 is the first number in subsequent rows which is smaller than 4.

Any suggestion? package?


Solution

  • Well it's not pretty and probably not super efficient, but it seems to get the job done. Here we go ...

    newcols <- with(data, {
        lapply(seq_along(number), function(i) {
            x <- number[-(1:i)][-i][1]
            c(x, abs(datetime[i] - datetime[number == x])[1])
        })
    })
    
    setNames(
        cbind(data[1], do.call(rbind, newcols)), 
        c(names(data)[1], "nextsmallest", "timediff")
    )
    #   number nextsmallest timediff
    # 1      4            3        7
    # 2      5            3        5
    # 3      3            1        1
    # 4      1            0        2
    # 5      0           NA       NA