Filter all days between a time range in R

后端 未结 2 867
孤城傲影
孤城傲影 2021-01-13 14:47

I have a data frame like below:

entry_no      id            time
_________     ___           _____
1              1        2016-09-01 09:30:09
2                      


        
相关标签:
2条回答
  • 2021-01-13 15:20

    I would suggest using lubridate package.

    library(lubridate)
    
    date1 <- as.POSIXct("2016-09-01 09:00:00") #lower bound
    date2 <- as.POSIXct("2016-09-01 10:00:00") #upper bound
    rng <- new_interval(date1, date2)          #desired range
    
    result <- df[df$time %within% rng,]
    

    You may need to check the class of time variable and apply some changes before using the above:

    df$time <- as.POSIXct(df$time)
    
    0 讨论(0)
  • 2021-01-13 15:35

    You could achieve it with a bit of simple formatting:

    dat$hms <- format(as.POSIXct(dat$time), "%H:%M:%S")
    dat[dat$hms >= "09:00:00" & dat$hms <= "10:00:00",]
    
    #  entry_no id                time      hms
    #1        1  1 2016-09-01 09:30:09 09:30:09
    
    0 讨论(0)
提交回复
热议问题