[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????
Gavin Simpson
gavin.simpson at ucl.ac.uk
Tue Sep 15 18:28:02 CEST 2009
On Tue, 2009-09-15 at 17:16 +0100, Corrado wrote:
> Dear R,
>
> the condition:
>
> identical(length(x),1) returns FALSE
>
> but
>
> print(length(x))
>
> returns 1 and:
>
> is.vector(x) is TRUE.
> is.integer(length(x)) is TRUE
> length(x) ==1 is TRUE
>
> I am puzzled.
The key is noticing that 1 is a double:
[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]
> x <- 2
> identical(length(x),1)
[1] FALSE
> class(1)
[1] "numeric"
> typeof(1)
[1] "double"
> typeof(length(x))
[1] "integer"
and therein lies the reason for the difference.
If you want to force 1 to be integer, the append and L, i.e.:
> identical(length(x),1L)
[1] TRUE
Are you really looking for all.equal()?
> isTRUE(all.equal(length(x), 1))
[1] TRUE
HTH
G
>
> Regards
--
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Dr. Gavin Simpson [t] +44 (0)20 7679 0522
ECRC, UCL Geography, [f] +44 (0)20 7679 0565
Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk
Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/
UK. WC1E 6BT. [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
More information about the R-help
mailing list