[R] date comparison doesn't match value
Sarah Goslee
sarah.goslee at gmail.com
Fri Oct 7 18:58:39 CEST 2016
Hi Heather,
I can't duplicate your problem: "looks like a date" is not helpful. If
you use dput() to provide actual data, then maybe we can actually help
you. Providing sessionInfo() would also help, as some time problems
may be OS-related.
Meanwhile, see if working thru this can help you get it sorted out.
mydatetxt <- "2011-07-01 12:08:07 EDT"
mydatect <- as.POSIXct(mydatetxt, tz="EDT")
str(mydatetxt)
str(mydatect)
mydatetxt < "1310-01-01 00:00:00 EDT"
mydatect < "1310-01-01 00:00:00 EDT"
mydatetxt < "1309-12-31 23:59:59 EDT"
mydatect < "1309-12-31 23:59:59 EDT"
# more useful
mydatetxt < "2009-12-31 23:59:59 EDT"
mydatetxt < "2011-12-31 23:59:59 EDT"
mydatect < "2009-12-31 23:59:59 EDT"
mydatect < "2011-12-31 23:59:59 EDT"
mydatect < as.POSIXct("2009-12-31 23:59:59 EDT", tz="EDT")
mydatect < as.POSIXct("2011-12-31 23:59:59 EDT", tz="EDT")
# as run:
> sessionInfo()
R version 3.3.1 (2016-06-21)
Platform: x86_64-redhat-linux-gnu (64-bit)
Running under: Fedora 23 (Twenty Three)
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
LC_TIME=en_US.UTF-8
[4] LC_COLLATE=en_US.UTF-8 LC_MONETARY=en_US.UTF-8
LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
LC_ADDRESS=C
[10] LC_TELEPHONE=C LC_MEASUREMENT=en_US.UTF-8
LC_IDENTIFICATION=C
> mydatetxt <- "2011-07-01 12:08:07 EDT"
> mydatect <- as.POSIXct(mydatetxt, tz="EDT")
>
> str(mydatetxt)
chr "2011-07-01 12:08:07 EDT"
> str(mydatect)
POSIXct[1:1], format: "2011-07-01 12:08:07"
>
> mydatetxt < "1310-01-01 00:00:00 EDT"
[1] FALSE
> mydatect < "1310-01-01 00:00:00 EDT"
[1] FALSE
>
> mydatetxt < "1309-12-31 23:59:59 EDT"
[1] FALSE
> mydatect < "1309-12-31 23:59:59 EDT"
[1] FALSE
>
>
> # more useful
>
> mydatetxt < "2009-12-31 23:59:59 EDT"
[1] FALSE
> mydatetxt < "2011-12-31 23:59:59 EDT"
[1] TRUE
>
> mydatect < "2009-12-31 23:59:59 EDT"
[1] FALSE
> mydatect < "2011-12-31 23:59:59 EDT"
[1] TRUE
>
> mydatect < as.POSIXct("2009-12-31 23:59:59 EDT", tz="EDT")
[1] FALSE
> mydatect < as.POSIXct("2011-12-31 23:59:59 EDT", tz="EDT")
[1] TRUE
>
On Fri, Oct 7, 2016 at 12:41 PM, Simon, Heather <Simon.Heather at epa.gov> wrote:
> 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
>
>
More information about the R-help
mailing list