r - How do I compare one row to the next? -
i want able compare values of 2 variables of on 30 variables. illustration info :
a, b 0, 2014-05-01 13 30, 2014-05-02 14 30, 2014-05-02 15 60, 2014-05-02 15 30, 2014-05-03 08 60, 2014-05-03 09 a number. b timestamp in format - %y-%m-%d %h . now, extract rows in date 1 row next same. and, hr within 1 hr of row above. (so, row if date 14th may , 9 am. , row + 1 date 14th may , hr 8 or 9 or 10. then, pulled out in separate info frame, rbinding each row satisfies condition.
running on above data, result
30, 2014-05-02 15 60, 2014-05-02 15 60, 2014-05-03 09
this job lubridate:
dat <- data.frame(a = c(0,30,30,60,30,60), b = c("2014-05-01 13","2014-05-02 14","2014-05-02 15","2014-05-02 15","2014-05-03 08","2014-05-03 09"), stringsasfactors = false) library(dplyr) library(lubridate) you can utilize lag pull out previous value of b, , utilize dhours advance 1 hour:
dat %>% mutate(b = ymd_h(b), close = b <= lag(b + dhours(1))) %>% filter(close) b close 1 30 2014-05-02 15:00:00 true 2 60 2014-05-02 15:00:00 true 3 60 2014-05-03 09:00:00 true r
No comments:
Post a Comment