[R] puzzling Date math result
Chabot, Denis
Denis.Chabot at dfo-mpo.gc.ca
Wed Apr 18 03:27:30 CEST 2012
Thank you both to Michael and Calum,
Your comments pointed me to the answer.
I did not succeed in making a useful reproducible example using dput(), so I'll have to study that command further. But it is no longer necessary for this thread to have a successful end.
Your comments about floating point gremlins, and the reminder that as.integer (15070.1) is 15070, made me realize that I should have used as.numeric(), not as.integer(), to see what was going on.
The two dates were indeed different, one was 15070.3. I went back to the many operations I did to create the offending dataframe and realized that I aggregated two small samples together and attributed to them the mean date (literally using the mean function on the 2 dates). As the result looked normal in Date format, I thought all was well.
Lesson learned. Thanks again and sorry to have been a bit quick to resort to the list on this one.
Denis
Le 2012-04-17 à 12:53, R. Michael Weylandt a écrit :
> A slightly quieter response:
>
> Please use dput() to create a reproducible example -- for this case,
> if x and y aren't too long, it seems that dput(x) and dput(y) would
> comprise one. str() helps (and thank you for that -- it gave me a
> place to start), but it doesn't provide quite enough to reproduce
> here.
>
> My guess is that R FAQ 7.31 is in play here... note that
> storage.mode(as.Date(Sys.Date())) is double not integer, so you need
> to keep an eye on the floating point gremlins.
>
> Michael
>
> On Tue, Apr 17, 2012 at 12:25 PM, Calum Polwart
> <yahoo at wittongilbert.free-online.co.uk> wrote:
>>
>>
>> On Tue, 17 Apr 2012 10:44:40 -0400, Denis Chabot wrote:
>>
>>> Hi,
>>>
>>
>>> I cannot make a reproducible example easily for my problem, so I'll
>>>
>> describe it as best as I can.
>>
>> YOU KIND OF NEED ONE...
>>
>>>>
>> a=test1$période[21]
>>>
>>>> b=test2$date[22]
>>> a f
>>> argin-left:5px;
>> width:100%">b
>>>
>>> [1] "2011-04-06"
>>>
>>>>
>>
>> THIS IS WHY YOU NEED TO
>> GIVE US A REPRODUCABLE MEANS BECAUSE IF I SIMPLY DO A="2011-04-06" AND
>> B="2011-04-06" I'M GOING TO GET A==B TRUE
>>
>> WHERE IS THE DATA IN THE DF'S
>> BEING SOURCED FROM? IS IT A DATABASE? COULD THERE BE DIFFERENT DATA
>> DEFINITIONS? IS ITS A CSV FILE ETC?
>>
>> and then thi> ft:#1010ff 2px
>> solid; margin-left:5px; width:100%">as.integer(a)
>>>
>>> [1] 15070
>>>
>>>>
>> as.integer(b)
>>>
>>> [1] 15070
>>>
>>> SUGGESTION THAT MIGHT POINT YOU IN A
>> DIRECTION...
>>>
>>> as.integer (15070)
>> pre>
>>
>> as.integer (15070.1)
>>
>> [1]
>> 15070
>>
>> as.integer (15070)==as.integer(15070.1)
>>
>> [1]
>> TRUE
>>
>> 15070==15070.1
>>
>> [1] FALSE
>>
>> SECOND SUGGESTION WOULD BE TO DROP THE
>> É NO IDEA WHY THAT WOULD CAUSE A PROBLEM (OR WHERE), BUT ANYTHING THATS
>> NOT A-Z OR A-Z ONLY INTRODUCES A POTENTIAL EXTRA HEADACHE...
>>
>>
>> as.integer(a)==as.integer(b)
>>
>>>
>>
>> [[alternative HTML version deleted]]
>>
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
More information about the R-help
mailing list