Don't separate out the date from the time. When you use format
to get the time, you are converting it to a character
class, that doesn't know how to do time-based comparisons with >
and <
.
Instead, use hour
to extract the hour component as an integer, and do comparisons on that:
library(lubridate)
Th %>%
mutate(DateTime <- as.POSIXct(date, "%Y-%m-%d %H:%M:%S", tz = "")) %>%
filter(hour(DateTime) >= 6 & hour(DateTime ) < 18)
I'm making some assumptions about your data structure - if you need more help than this please edit your question to share some sample data with dput()
as the commenters have requested. dput(Th[1:5, ])
should be plenty.
Note that if you want to do a lot of operations on just the times (ignoring the date part), you could use the times
class from the chron
package, see here for some more info.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…