1

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

  1. Is there a simple way to do this?
  2. How could this be done just using POSIXct without resorting to xts?
user1320502
  • 2,510
  • 5
  • 28
  • 46

1 Answers1

0

the bets solution i have is:

frame[ paste0("::", ref$Date - 24*60*60) ]
user1320502
  • 2,510
  • 5
  • 28
  • 46
  • You can use `head` with a negative `n`: `head(frame[paste0("/",ref$Date)], -1)` – GSee Feb 11 '14 at 13:18
  • Thanks GSee but I may generally have many matches for the same day and I won't necessarily know what to set `n` for the negative head. although i guess i could get this from `nrow(frame[ paste0(":", ref$Date) ])` – user1320502 Feb 11 '14 at 14:35