Some example data.....
library(xts)
ref <- data.frame(Date = c("2/1/2000"))
frame <- read.table(text = " x Date Time
a 1/1/2000 5:00
a 1/1/2000 18:00
a 2/1/2000 7:00",
stringsAsFactors = FALSE,
header = TRUE
)
ref$Date <- as.POSIXct(ref$Date, format = "%d/%m/%Y")
frame$datetime <- paste(frame $Date,frame $Time, sep = " ")
frame$datetime <- as.POSIXct(frame $datetime, format = "%d/%m/%Y %H:%M")
frame <- xts(frame[, 1], order.by= frame [, 4] )
I can get all the days in frame that match ref using..
> frame[ paste0(":", ref$Date) ]
[,1]
2000-01-02 07:00:00 "a" ]
and all the days before and including using
> frame[ paste0("::", ref$Date) ]
[,1]
2000-01-01 05:00:00 "a"
2000-01-01 18:00:00 "a"
2000-01-02 07:00:00 "a"
but if I just want the days before I cannot do:
> frame[ !(paste0(ref$Date, "::")) ]
Error in !(paste0(ref$Date, "::")) : invalid argument type
QUESTIONS
- Is there a simple way to do this?
- How could this be done just using POSIXct without resorting to
xts
?