Hi, I wonder if the following (apparent) inconsistency is a bug or feature. Since scalars are simply vectors of length one I would think that a and a:a produce the same result. For example,> identical(4.01,4.01:4.01)[1] TRUE However, identical(4,4:4) [1] FALSE and> identical(4.0,4.0:4.0)[1] FALSE A closer look reveals that the colon operator produces objects of different class, e.g.> class(4)[1] "numeric"> class(4.0)[1] "numeric" but> class(4:4)[1] "integer"> class(4.0:4.0)[1] "integer" Georgi Boshnakov ------------------------------------------------------------------------------ Dr Georgi Boshnakov tel.: +44 (0)161 200 3684 Mathematics Department email: georgi.boshnakov@umist.ac.uk UMIST P O Box 88 Manchester M60 1QD UK [[alternative HTML version deleted]]
See ?":", especially the `Value' section. Numeric literals in R is double by default. Andy> From: Georgi Boshnakov > > Hi, > > I wonder if the following (apparent) inconsistency is a bug > or feature. > Since scalars are simply vectors of length one I would think that > a and > a:a > produce the same result. For example, > > > identical(4.01,4.01:4.01) > [1] TRUE > > However, > > identical(4,4:4) > [1] FALSE > > and > > > identical(4.0,4.0:4.0) > [1] FALSE > > A closer look reveals that the colon operator produces > objects of different class, e.g. > > > class(4) > [1] "numeric" > > class(4.0) > [1] "numeric" > > but > > > class(4:4) > [1] "integer" > > class(4.0:4.0) > [1] "integer" > > > Georgi Boshnakov > > -------------------------------------------------------------- > ---------------- > Dr Georgi Boshnakov tel.: +44 > (0)161 200 3684 > Mathematics Department email: > georgi.boshnakov at umist.ac.uk > UMIST > P O Box 88 > Manchester M60 1QD > UK > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >
On Mon, 2004-08-02 at 09:46, Georgi Boshnakov wrote:> Hi, > > I wonder if the following (apparent) inconsistency is a bug or > feature. > Since scalars are simply vectors of length one I would think that > a and > a:a > produce the same result. For example, > > > identical(4.01,4.01:4.01) > [1] TRUE > > However, > > identical(4,4:4) > [1] FALSE > > and > > > identical(4.0,4.0:4.0) > [1] FALSE > > A closer look reveals that the colon operator produces objects of > different class, e.g. > > > class(4) > [1] "numeric" > > class(4.0) > [1] "numeric" > > but > > > class(4:4) > [1] "integer" > > class(4.0:4.0) > [1] "integer" > > > Georgi BoshnakovThe ":" operator is the functional equivalent of "seq(from=a, to=b)". Note that the help for seq() indicates the following for the return value: "The result is of mode "integer" if from is (numerically equal to an) integer and by is not specified." Thus, when using the ":" operator, you get integers as the returned value(s), which is what is happening in your final pair of examples. If you look at the final example under ?identical, you will see: identical(1, as.integer(1)) ## FALSE, stored as different types This is because the first 1 is a double by default. Thus, in the case of: identical(4, 4:4) the first 4 is of type double, while the 4:4 is of type single. Thus the result is FALSE. Now, on the other hand, try:> typeof(seq(4, 4, by = 1))[1] "double" You see that the result of the sequence is of type double. Hence:> identical(4, seq(4, 4, by = 1))[1] TRUE So to the question in your subject, no "k" (a double by default) is not the same as "k:k" (a integer by default). HTH, Marc Schwartz
On Mon, 2004-08-02 at 10:09, Marc Schwartz wrote: <snip>> Thus, in the case of: > > identical(4, 4:4) > > the first 4 is of type double, while the 4:4 is of type single. Thus the > result is FALSE.<snip> Correction. The above sentence should read: the first 4 is of type double, while the 4:4 is of type **INTEGER**. Thus the result is FALSE. Sorry about that. Need more coffee.... Marc
>>>>> "Georgi" == Georgi Boshnakov <georgi.boshnakov at umist.ac.uk> >>>>> on Mon, 2 Aug 2004 15:46:37 +0100 writes:Georgi> Hi, I wonder if the following (apparent) Georgi> inconsistency is a bug or feature. a feature "of course". Georgi> Since scalars are simply vectors of length one I Georgi> would think that a and a:a produce the same result. Why would you? Have you read the documentation for ":" carefully? {Part this topic, namely that a numeric constant such as "4" is a "double" in R (but integer in S-plus since S+5.0) should probably finally end up in the R FAQ ... } Regards, Martin Maechler Georgi> For example, >> identical(4.01,4.01:4.01) Georgi> [1] TRUE Georgi> However, Georgi> identical(4,4:4) Georgi> [1] FALSE Georgi> and >> identical(4.0,4.0:4.0) Georgi> [1] FALSE Georgi> A closer look reveals that the colon operator produces objects of different class, e.g. a closer look wouldn't have been necessary had you read and understood the documentation. >> class(4) Georgi> [1] "numeric" >> class(4.0) Georgi> [1] "numeric" Georgi> but >> class(4:4) Georgi> [1] "integer" >> class(4.0:4.0) Georgi> [1] "integer" Georgi> Georgi Boshnakov Georgi> ------------------------------------------------------------------------------ Georgi> Dr Georgi Boshnakov tel.: +44 (0)161 200 3684 Georgi> Mathematics Department email: georgi.boshnakov at umist.ac.uk Georgi> UMIST Georgi> P O Box 88 Georgi> Manchester M60 1QD Georgi> UK Georgi> [[alternative HTML version deleted]] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ do read the posting guide why you should reconfigure your e-mail software