R: how to filter a timestamp by hour and minute?

前端 未结 5 760
粉色の甜心
粉色の甜心 2020-12-21 00:47

I am struggling with the following example

time = c(\'2013-01-03 21:59:21.549\', \'2013-01-04 22:00:21.549\', \'2013-01-05 22:01:21.222\', \'2013-01-06 22:06         


        
5条回答
  •  失恋的感觉
    2020-12-21 01:02

    Another idea would be the following. You can create a numeric vector using hour, minute, and second. You can extract them with format() and convert character to numeric. Then, you subset the data with the two numbers indicating the time range you want (i.e., 215900, 220100).

    library(dplyr)
    
    data %>%
    mutate(foo = as.numeric(format(time, "%H%M%S"))) %>%
    filter(between(foo, 215900, 220100)) %>%
    select(-foo)
    
    #                 time value
    #                
    #1 2013-01-03 21:59:21     1
    #2 2013-01-04 22:00:21     2
    

提交回复
热议问题