Un texte encapsul? et encod? dans un jeu de caract?res inconnu a ?t? nettoy?... Nom : non disponible URL : <https://stat.ethz.ch/pipermail/r-help/attachments/20081202/10f66fc6/attachment.pl>
date operations
8 messages · Christophe Dutang, jim holtman, Gabor Grothendieck +1 more
'str' is your friend. Look at the results:
alpha2=as.Date("2008-12-21")
alpha1=as.Date("2008-12-21")-as.Date("2008-10-26")
alpha1
Time difference of 56 days
alpha2
[1] "2008-12-21"
alpha1+alpha2
Time difference of 14290 days
Warning message:
Incompatible methods ("Ops.difftime", "+.Date") for "+"
str(alpha1)
Class 'difftime' atomic [1:1] 56 ..- attr(*, "units")= chr "days"
str(alpha2)
Class 'Date' num 14234
alpha1 is not of the Date class.
On Tue, Dec 2, 2008 at 4:10 PM, Christophe Dutang <dutangc at gmail.com> wrote:
Hi all,
I'm dealing with dates in R (2.7.2), but some basic operations raise a
warning.
Incompatible methods ("+.Date", "Ops.difftime") for "+"
I saw this topic in this mailing list, but I do not understand what to
do...
cf. https://stat.ethz.ch/pipermail/r-help/2008-June/165842.html
Do I have to convert Dates to numeric?
Thanks in advance
Christophe
PS : R script used
alpha2=as.Date("2008-12-21")
alpha1=as.Date("2008-12-21")-as.Date("2008-10-26")
alpha1
alpha2
alpha1+alpha2
[[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.
Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
If you want to do the addition, 'unclass' the variable:
alpha2+4
[1] "2008-12-25"
alpha2 + unclass(alpha1)
[1] "2009-02-15"
On Tue, Dec 2, 2008 at 4:10 PM, Christophe Dutang <dutangc at gmail.com> wrote:
Hi all,
I'm dealing with dates in R (2.7.2), but some basic operations raise a
warning.
Incompatible methods ("+.Date", "Ops.difftime") for "+"
I saw this topic in this mailing list, but I do not understand what to
do...
cf. https://stat.ethz.ch/pipermail/r-help/2008-June/165842.html
Do I have to convert Dates to numeric?
Thanks in advance
Christophe
PS : R script used
alpha2=as.Date("2008-12-21")
alpha1=as.Date("2008-12-21")-as.Date("2008-10-26")
alpha1
alpha2
alpha1+alpha2
[[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.
Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
Thanks a lot Envoy? de mon iPhone Le 3 d?c. 08 ? 00:39, "jim holtman" <jholtman at gmail.com> a ?crit :
If you want to do the addition, 'unclass' the variable:
alpha2+4
[1] "2008-12-25"
alpha2 + unclass(alpha1)
[1] "2009-02-15"
On Tue, Dec 2, 2008 at 4:10 PM, Christophe Dutang <dutangc at gmail.com> wrote:
Hi all,
I'm dealing with dates in R (2.7.2), but some basic operations
raise a
warning.
Incompatible methods ("+.Date", "Ops.difftime") for "+"
I saw this topic in this mailing list, but I do not understand what
to
do...
cf. https://stat.ethz.ch/pipermail/r-help/2008-June/165842.html
Do I have to convert Dates to numeric?
Thanks in advance
Christophe
PS : R script used
alpha2=as.Date("2008-12-21")
alpha1=as.Date("2008-12-21")-as.Date("2008-10-26")
alpha1
alpha2
alpha1+alpha2
[[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.
-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
What is odd is that it seems to run ok if we call "+.Date" directly:
"+.Date"(alpha1, alpha2)
[1] "2009-02-15"
On Tue, Dec 2, 2008 at 4:10 PM, Christophe Dutang <dutangc at gmail.com> wrote:
Hi all,
I'm dealing with dates in R (2.7.2), but some basic operations raise a
warning.
Incompatible methods ("+.Date", "Ops.difftime") for "+"
I saw this topic in this mailing list, but I do not understand what to
do...
cf. https://stat.ethz.ch/pipermail/r-help/2008-June/165842.html
Do I have to convert Dates to numeric?
Thanks in advance
Christophe
PS : R script used
alpha2=as.Date("2008-12-21")
alpha1=as.Date("2008-12-21")-as.Date("2008-10-26")
alpha1
alpha2
alpha1+alpha2
[[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.
On Wed, 2008-12-03 at 13:18 -0500, Gabor Grothendieck wrote:
What is odd is that it seems to run ok if we call "+.Date" directly:
"+.Date"(alpha1, alpha2)
[1] "2009-02-15"
It also works if you flip the ordering:
alpha2 + alpha1
[1] "2009-02-15"
Warning message:
Incompatible methods ("+.Date", "Ops.difftime") for "+"
It is just a warning, but one should check that the answer is correct
given the perceived problem with different classes of objects.
Why it works is not odd if you look at the help for ?`+.Date`, which
shows that for this method (correct term?) we need 'date' + 'x', where
'date' is an object of class Date and 'x' is numeric.
The OP can avoid the warning by:
alpha2 + as.numeric(alpha1)
[1] "2009-02-15" If you put alpha1 first (class difftime) that method gets used and alpha2 is coerced to numeric to complete the operation:
alpha1+alpha2
Time difference of 14290 days
Warning message:
Incompatible methods ("Ops.difftime", "+.Date") for "+"
as.numeric(alpha2) + 56
[1] 14290 To answer the OP's question about conversion, no you don't need to convert Date objects to numerics to work with them. You just need to be aware of method dispatch for `+()` and check that adding objects of differing classes makes sense. Of course, this assumes that the OP wanted the answer as an object of class "Date"...? G
On Tue, Dec 2, 2008 at 4:10 PM, Christophe Dutang <dutangc at gmail.com> wrote:
Hi all,
I'm dealing with dates in R (2.7.2), but some basic operations raise a
warning.
Incompatible methods ("+.Date", "Ops.difftime") for "+"
I saw this topic in this mailing list, but I do not understand what to
do...
cf. https://stat.ethz.ch/pipermail/r-help/2008-June/165842.html
Do I have to convert Dates to numeric?
Thanks in advance
Christophe
PS : R script used
alpha2=as.Date("2008-12-21")
alpha1=as.Date("2008-12-21")-as.Date("2008-10-26")
alpha1
alpha2
alpha1+alpha2
[[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.
______________________________________________ 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.
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Dr. Gavin Simpson [t] +44 (0)20 7679 0522 ECRC, UCL Geography, [f] +44 (0)20 7679 0565 Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/ UK. WC1E 6BT. [w] http://www.freshwaters.org.uk %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 197 bytes Desc: This is a digitally signed message part URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20081203/c2cd375f/attachment.bin>
On Wed, Dec 3, 2008 at 1:57 PM, Gavin Simpson <gavin.simpson at ucl.ac.uk> wrote:
On Wed, 2008-12-03 at 13:18 -0500, Gabor Grothendieck wrote:
What is odd is that it seems to run ok if we call "+.Date" directly:
"+.Date"(alpha1, alpha2)
[1] "2009-02-15"
It also works if you flip the ordering:
alpha2 + alpha1
[1] "2009-02-15"
Warning message:
Incompatible methods ("+.Date", "Ops.difftime") for "+"
It is just a warning, but one should check that the answer is correct
given the perceived problem with different classes of objects.
Why it works is not odd if you look at the help for ?`+.Date`, which
shows that for this method (correct term?) we need 'date' + 'x', where
'date' is an object of class Date and 'x' is numeric.
In fact what it says is that x may be numeric OR difftime. Also the above does not explain it in any case because:
"+.Date"(alpha1, alpha2)
[1] "2009-02-15"
alpha1+alpha2
Time difference of 14290 days
Warning message:
Incompatible methods ("Ops.difftime", "+.Date") for "+"
The OP can avoid the warning by:
alpha2 + as.numeric(alpha1)
[1] "2009-02-15"
His question was how to avoid using as.numeric.
If you put alpha1 first (class difftime) that method gets used and alpha2 is coerced to numeric to complete the operation:
alpha1+alpha2
Time difference of 14290 days
Warning message:
Incompatible methods ("Ops.difftime", "+.Date") for "+"
as.numeric(alpha2) + 56
[1] 14290 To answer the OP's question about conversion, no you don't need to convert Date objects to numerics to work with them. You just need to be aware of method dispatch for `+()` and check that adding objects of differing classes makes sense. Of course, this assumes that the OP wanted the answer as an object of class "Date"...? G
On Tue, Dec 2, 2008 at 4:10 PM, Christophe Dutang <dutangc at gmail.com> wrote:
Hi all,
I'm dealing with dates in R (2.7.2), but some basic operations raise a
warning.
Incompatible methods ("+.Date", "Ops.difftime") for "+"
I saw this topic in this mailing list, but I do not understand what to
do...
cf. https://stat.ethz.ch/pipermail/r-help/2008-June/165842.html
Do I have to convert Dates to numeric?
Thanks in advance
Christophe
PS : R script used
alpha2=as.Date("2008-12-21")
alpha1=as.Date("2008-12-21")-as.Date("2008-10-26")
alpha1
alpha2
alpha1+alpha2
[[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.
______________________________________________ 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.
-- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Dr. Gavin Simpson [t] +44 (0)20 7679 0522 ECRC, UCL Geography, [f] +44 (0)20 7679 0565 Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/ UK. WC1E 6BT. [w] http://www.freshwaters.org.uk %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
On Wed, 2008-12-03 at 14:13 -0500, Gabor Grothendieck wrote:
<snip />
Why it works is not odd if you look at the help for ?`+.Date`, which shows that for this method (correct term?) we need 'date' + 'x', where 'date' is an object of class Date and 'x' is numeric.
In fact what it says is that x may be numeric OR difftime.
Yes, sorry, got distracted with dinner...
Also the above does not explain it in any case because:
"+.Date"(alpha1, alpha2)
[1] "2009-02-15"
I guess something like this is happening:
as.Date(as.numeric(alpha1) + as.numeric(alpha2), origin="1970-01-01")
[1] "2009-02-15" Although internally, `+.Date` deals with objects of class "difftime" for either date or x. So technically the help for ?`+.Date` is at odds with the code; although there is a degree of ambiguity in what a 'date object' is?
alpha1+alpha2
Time difference of 14290 days
Warning message:
Incompatible methods ("Ops.difftime", "+.Date") for "+"
But you are doing method dispatch yourself (by hand) in the first case and allowing `+` to deal with it in the latter. The warning is not coming from `+.Date`(). This looks like an infelicity in non-standard method dispatch that takes place within `+`, a primitive function. It would be nice if `+` didn't raise warnings about potential incompatible classes, but it can't know in advance what methods users might write for it.
The OP can avoid the warning by:
alpha2 + as.numeric(alpha1)
[1] "2009-02-15"
His question was how to avoid using as.numeric.
Well, I read it as not converting his "Date"s to numerics (I converted a "difftime"). But that is splitting hairs ;-) The OP can use `+.Date` directly as you suggest, or ignore the warnings as they are harmless, or coerce the difftime object to numeric as I suggested. It makes no odds in these cases. I guess the main point for the OP is to realise why the warning is there and determine if it is OK to ignore. G
If you put alpha1 first (class difftime) that method gets used and alpha2 is coerced to numeric to complete the operation:
alpha1+alpha2
Time difference of 14290 days
Warning message:
Incompatible methods ("Ops.difftime", "+.Date") for "+"
as.numeric(alpha2) + 56
[1] 14290 To answer the OP's question about conversion, no you don't need to convert Date objects to numerics to work with them. You just need to be aware of method dispatch for `+()` and check that adding objects of differing classes makes sense. Of course, this assumes that the OP wanted the answer as an object of class "Date"...? G
On Tue, Dec 2, 2008 at 4:10 PM, Christophe Dutang <dutangc at gmail.com> wrote:
Hi all,
I'm dealing with dates in R (2.7.2), but some basic operations raise a
warning.
Incompatible methods ("+.Date", "Ops.difftime") for "+"
I saw this topic in this mailing list, but I do not understand what to
do...
cf. https://stat.ethz.ch/pipermail/r-help/2008-June/165842.html
Do I have to convert Dates to numeric?
Thanks in advance
Christophe
PS : R script used
alpha2=as.Date("2008-12-21")
alpha1=as.Date("2008-12-21")-as.Date("2008-10-26")
alpha1
alpha2
alpha1+alpha2
[[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.
______________________________________________ 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.
-- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Dr. Gavin Simpson [t] +44 (0)20 7679 0522 ECRC, UCL Geography, [f] +44 (0)20 7679 0565 Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/ UK. WC1E 6BT. [w] http://www.freshwaters.org.uk %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Dr. Gavin Simpson [t] +44 (0)20 7679 0522 ECRC, UCL Geography, [f] +44 (0)20 7679 0565 Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/ UK. WC1E 6BT. [w] http://www.freshwaters.org.uk %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 197 bytes Desc: This is a digitally signed message part URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20081203/cf26854d/attachment.bin>