You need to convert your dates from characters into a Date
type with as.Date()
(or a POSIX type if you have more information like the time of day). Then you can make comparisons with standard relational operators such as <= and >=.
You should consider using a timeseries package such as zoo
for this.
Edit:
Just to respond to your comment, here's an example of using dates with your existing vector:
> as.Date(names(bar)) < as.Date("2001-10-14")
[1] TRUE FALSE FALSE
> bar[as.Date(names(bar)) < as.Date("2001-10-14")]
1997-10-14
1
Although you really should just use a time series package. Here's how you could do this with zoo
(or xts
, timeSeries
, fts
, etc.):
library(zoo)
ts <- zoo(c(1, 2, 1), as.Date(c("1997-10-14", "2001-10-14", "2007-10-14")))
ts[index(ts) < as.Date("2001-10-14"),]
Since the index is now a Date
type, you can make as many comparisons as you want. Read the zoo
vignette for more information.