[R] date comparison doesn't match value

Simon, Heather Simon.Heather at epa.gov
Fri Oct 7 18:41:30 CEST 2016


I am running into trouble when trying to compare date fields in my dataset.  When I view a field, it looks like it is a date in 2011:


> alldata$new.date.local[1]
[1] "2011-07-01 12:08:07 EDT"

However, when I try to compare it to a character string, it seems to think it is equal to sometime between the years 1310 and 1309

> alldata$new.date.local[1] < "1310-01-01 00:00:00 EDT"
[1] TRUE
> alldata$new.date.local[1] < "1309-12-31 23:59:59 EDT"
[1] FALSE

But not exactly equal to midnight of Dec 31 in 1309, so it is not equal to any exact time:

> alldata$new.date.local[1] == "1309-12-31 23:59:59 EDT"
[1] FALSE
> alldata$new.date.local[1] < "1309-12-31 23:59:59 EDT"
[1] FALSE
> alldata$new.date.local[1] > "1309-12-31 23:59:59 EDT"
[1] TRUE

Even though this date field was created using as.POSIXct from a text string, I have tried fixing this by reapplying the as.POSIXct function:

>alldata$new.date.local <- as.POSIXct(alldata$new.date.local, tz = "EDT")

But this does not seem to fix the problem.  When I try recreating the date from a character string I get the same behavior.  Any suggestions would be much appreciated.

-Heather



	[[alternative HTML version deleted]]



More information about the R-help mailing list