[R] identical(length(x), 1) returns FALSE, but print(length(x)) is 1, length(x)==1 is TRUE, and is.integer(lenght(x)) is TRUE????
William Dunlap
wdunlap at tibco.com
Tue Sep 15 21:45:02 CEST 2009
Should the help file for identical() say that it mainly
useful for testing code (as part of a QA program)
and it is not recommended for general use? Currently
help(identical) gives, IMO, inappropriate advice:
A call to identical is the way to test exact equality
in if and while statements, as well as in logical
expressions that use && or ||. In all these applications
you need to be assured of getting a single logical value.
Users often use the comparison operators, such as ==
or !=, in these situations. It looks natural, but it is not
what these operators are designed to do in R. They
return an object like the arguments. If you expected x
and y to be of length 1, but it happened that one of
them wasn't, you will not get a single FALSE. Similarly,
if one of the arguments is NA, the result is also NA.
In either case, the expression if(x == y).... won't work as expected.
That advice encourages folks to ignore logic or input errors,
in addition to this user's problem of identical() being too picky
about equality for his purposes.
I think that the user should be encouraged to use all(x==y)
or any(x==y), perhaps with na.rm=TRUE, in conditionals that
might involve non-scalars, not identical(x,y). If you expect
a scalar then leave off the any or all and learn that the warning
from if that the condition is not a scalar means real trouble
(should it be a fatal error?). || and && should give such a warning
or error but do not.
Bill Dunlap
TIBCO Software Inc - Spotfire Division
wdunlap tibco.com
> -----Original Message-----
> From: r-help-bounces at r-project.org
> [mailto:r-help-bounces at r-project.org] On Behalf Of Thomas Lumley
> Sent: Tuesday, September 15, 2009 10:12 AM
> To: Corrado
> Cc: r-help at r-project.org; gavin.simpson at ucl.ac.uk
> Subject: Re: [R] identical(length(x), 1) returns FALSE, but
> print(length(x)) is 1, length(x)==1 is TRUE, and
> is.integer(lenght(x)) is TRUE????
>
> On Tue, 15 Sep 2009, Corrado wrote:
>
> > On Tuesday 15 September 2009 17:28:02 Gavin Simpson wrote:
> >> [note you don't give us your x so I'm making this up - This is what
> >> Duncan was going on about in an earlier thread, give us
> something we can
> >> just paste into R and it works]
> >
> > Dear Gavin,
> >
> > I do not understand what more information! Take any vector
> of length 1, for
> > example x<-1. Plus all the command that where in my
> previous email ....
> >
> > What is the logic behind
> >
> > identical(length(x),1)
> >
> > being false?
> >
>
> length(x) is an integer, and 1 is a floating point number.
> They aren't identical, just equal, so
> identical(length(x), 1) is FALSE and length(x)==1 is TRUE.
>
> identical(length(x), 1L) should be TRUE, since 1L is the way
> to specify an integer value of 1.
>
>
> -thomas
>
> Thomas Lumley Assoc. Professor, Biostatistics
> tlumley at u.washington.edu University of Washington, Seattle
>
> ______________________________________________
> 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.
>
More information about the R-help
mailing list