On 09/04/16 16:24, Jeff Newmiller wrote:> I highly recommend making friends with the str function. Try > > str( 1 ) > str( 1:2 )Interesting. But to me counter-intuitive. Since R makes no distinction between scalars and vectors of length 1 (or more accurately I think, since in R there is *no such thing as a scalar*, only a vector of length 1) I don't see why "1" should be treated in a manner that is categorically different from the way in which "1:2" is treated. Can you, or someone else with deep insight into R and its rationale, explain the basis for this difference in treatment?> for the clue you need, and then > > sapply( 1:2, identical, 1L )cheers, Rolf -- Technical Editor ANZJS Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276
On 09/04/2016 6:27 AM, Rolf Turner wrote:> On 09/04/16 16:24, Jeff Newmiller wrote: >> I highly recommend making friends with the str function. Try >> >> str( 1 ) >> str( 1:2 ) > > Interesting. But to me counter-intuitive. Since R makes no distinction > between scalars and vectors of length 1 (or more accurately I think, > since in R there is *no such thing as a scalar*, only a vector of length > 1) I don't see why "1" should be treated in a manner that is > categorically different from the way in which "1:2" is treated. > > Can you, or someone else with deep insight into R and its rationale, > explain the basis for this difference in treatment?It's not the fact that one is a vector, it's just that the : function returns an integer result when given whole number arguments. The literal 1 is stored in floating point, the result of 1:2 is integer. I think the rationale is that sequences of whole numbers are often used as integers (e.g. in "for (i in 1:2)", whereas constants are often used in floating point expressions, so this reduces the number of conversions that are needed. If you really mean your 1 to be stored as an integer, write it as 1L. If you want it as floating point, write it as 1. Duncan Murdoch
Ok, I see the difference between 1 and 1:2, I'll just leave it as one of those "only in R" things. But it seems then, that as.numeric() should guarantee a FALSE outcome, yet it does not. To build on what Rolf pointed out, I would really love for someone to explain this one:> str(1)num 1> str(1:2)int [1:2] 1 2> str(as.numeric(1:2))num [1:2] 1 2> str(as(1:2,"numeric"))int [1:2] 1 2 Which doubly makes no sense. 1) Either the class is "numeric" or it isn't; I did not call as.integer() here. 2) method of recasting should not affect final class. Thanks, Ariel -----Original Message----- From: Rolf Turner [mailto:r.turner at auckland.ac.nz] Sent: Saturday, April 09, 2016 5:27 AM To: Jeff Newmiller Cc: Paulson, Ariel; 'r-help at r-project.org' Subject: Re: [FORGED] Re: [R] identical() versus sapply() On 09/04/16 16:24, Jeff Newmiller wrote:> I highly recommend making friends with the str function. Try > > str( 1 ) > str( 1:2 )Interesting. But to me counter-intuitive. Since R makes no distinction between scalars and vectors of length 1 (or more accurately I think, since in R there is *no such thing as a scalar*, only a vector of length 1) I don't see why "1" should be treated in a manner that is categorically different from the way in which "1:2" is treated. Can you, or someone else with deep insight into R and its rationale, explain the basis for this difference in treatment?> for the clue you need, and then > > sapply( 1:2, identical, 1L )cheers, Rolf -- Technical Editor ANZJS Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276
Indeed! Slightly simplified to emphasize your point:> class(as(1:2,"numeric"))[1] "integer"> class(as.numeric(1:2))[1] "numeric" whereas in ?as it says: "Methods are pre-defined for coercing any object to one of the basic datatypes. For example, as(x, "numeric") uses the existing as.numeric function. " I suspect this is related to my ignorance of S4 classes (i.e. as() ) and how they relate to S3 classes, but I certainly don't get it either. Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Mon, Apr 11, 2016 at 9:30 AM, Paulson, Ariel <apa at stowers.org> wrote:> Ok, I see the difference between 1 and 1:2, I'll just leave it as one of those "only in R" things. > > But it seems then, that as.numeric() should guarantee a FALSE outcome, yet it does not. > > To build on what Rolf pointed out, I would really love for someone to explain this one: > >> str(1) > num 1 > >> str(1:2) > int [1:2] 1 2 > >> str(as.numeric(1:2)) > num [1:2] 1 2 > >> str(as(1:2,"numeric")) > int [1:2] 1 2 > > Which doubly makes no sense. 1) Either the class is "numeric" or it isn't; I did not call as.integer() here. 2) method of recasting should not affect final class. > > Thanks, > Ariel > > > -----Original Message----- > From: Rolf Turner [mailto:r.turner at auckland.ac.nz] > Sent: Saturday, April 09, 2016 5:27 AM > To: Jeff Newmiller > Cc: Paulson, Ariel; 'r-help at r-project.org' > Subject: Re: [FORGED] Re: [R] identical() versus sapply() > > On 09/04/16 16:24, Jeff Newmiller wrote: >> I highly recommend making friends with the str function. Try >> >> str( 1 ) >> str( 1:2 ) > > Interesting. But to me counter-intuitive. Since R makes no distinction between scalars and vectors of length 1 (or more accurately I think, since in R there is *no such thing as a scalar*, only a vector of length > 1) I don't see why "1" should be treated in a manner that is categorically different from the way in which "1:2" is treated. > > Can you, or someone else with deep insight into R and its rationale, explain the basis for this difference in treatment? > >> for the clue you need, and then >> >> sapply( 1:2, identical, 1L ) > > cheers, > > Rolf > > -- > Technical Editor ANZJS > Department of Statistics > University of Auckland > Phone: +64-9-373-7599 ext. 88276 > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.