[R] [FORGED] Re: identical() versus sapply()
Duncan Murdoch
murdoch.duncan at gmail.com
Sat Apr 9 13:40:41 CEST 2016
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
More information about the R-help
mailing list