Sorry if this has been answered elsewhere, but I can't find any discussion of it. Wondering why the following situation occurs (duplicated on 3.2.2 CentOS6 and 3.0.1 Win2k, so I don't think it is a bug):> sapply(1, identical, 1)[1] TRUE> sapply(1:2, identical, 1)[1] FALSE FALSE> sapply(1:2, function(i) identical(as.numeric(i),1) )[1] TRUE FALSE> sapply(1:2, function(i) identical(as(i,"numeric"),1) )[1] FALSE FALSE I have been unable to find anything different about the versions of "1" that identical() is not finding identical. Thanks, Ariel [[alternative HTML version deleted]]
I highly recommend making friends with the str function. Try str( 1 ) str( 1:2 ) for the clue you need, and then sapply( 1:2, identical, 1L ) -- Sent from my phone. Please excuse my brevity. On April 8, 2016 3:24:31 PM PDT, "Paulson, Ariel" <apa at stowers.org> wrote:>Sorry if this has been answered elsewhere, but I can't find any >discussion of it. > >Wondering why the following situation occurs (duplicated on 3.2.2 >CentOS6 and 3.0.1 Win2k, so I don't think it is a bug): > >> sapply(1, identical, 1) >[1] TRUE > >> sapply(1:2, identical, 1) >[1] FALSE FALSE > >> sapply(1:2, function(i) identical(as.numeric(i),1) ) >[1] TRUE FALSE > >> sapply(1:2, function(i) identical(as(i,"numeric"),1) ) >[1] FALSE FALSE > >I have been unable to find anything different about the versions of "1" >that identical() is not finding identical. > >Thanks, >Ariel > > > > [[alternative HTML version deleted]] > >______________________________________________ >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.[[alternative HTML version deleted]]
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