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

前端 未结 5 767
粉色の甜心
粉色の甜心 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:04

    2019 is here! Here is a better (and simpler) solution using as.hms. The tz argument is mandatory.

        time_str = 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:23.559' )
        value = c(1,2,3,4)
        data <- tibble(time_str, value)
    
        data %>%  mutate(timestamp_utc = ymd_hms(time_str, tz = 'UTC'),
                         timestamp_est = with_tz(timestamp_utc, 'America/New_York'),
                                time_est = as.hms(timestamp_est, tz = 'America/New_York')) %>% 
          filter(time_est >= hms::as.hms('16:59:00', tz = 'America/New_York'),
                 time_est <= hms::as.hms('17:01:00', tz = 'America/New_York'))
    
    

    will do the job

    # A tibble: 2 x 5
      time_str                value timestamp_utc           timestamp_est           time_est 
                                                              

提交回复
热议问题