Corrado
2009-Sep-15 16:16 UTC
[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????
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. Regards -- Corrado Topi Global Climate Change & Biodiversity Indicators Area 18,Department of Biology University of York, York, YO10 5YW, UK Phone: + 44 (0) 1904 328645, E-mail: ct529 at york.ac.uk
Uwe Ligges
2009-Sep-15 16:22 UTC
[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????
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. > > Regards> is.integer(length(x)) [1] TRUE > is.integer(1) [1] FALSE Hence: > identical(length(x), 1L) [1] TRUE Uwe Ligges
Steve Lianoglou
2009-Sep-15 16:22 UTC
[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????
Hi, On Sep 15, 2009, at 12:16 PM, 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.But is.integer(1) is FALSE and identical(length(x), 1L) is TRUE ... ouch ... I guess the lesson learned is that: identical() != = -steve -- Steve Lianoglou Graduate Student: Computational Systems Biology | Memorial Sloan-Kettering Cancer Center | Weill Medical College of Cornell University Contact Info: http://cbio.mskcc.org/~lianos/contact
Gavin Simpson
2009-Sep-15 16:28 UTC
[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, 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 %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Gavin Simpson
2009-Sep-15 16:31 UTC
[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, 2009-09-15 at 12:22 -0400, Steve Lianoglou wrote:> Hi,<snip />> I guess the lesson learned is that: > > identical() != =Indeed, and ?identical explains some of the motivation behind the function identical and the examples of said help page include the problem the OP faced... now where was that posting guide... G -- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% 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 %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Gavin Simpson
2009-Sep-15 16:34 UTC
[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, 2009-09-15 at 17:28 +0100, Gavin Simpson wrote:> On Tue, 2009-09-15 at 17:16 +0100, Corrado wrote: > > Dear R,<snip />> > Are you really looking for all.equal()? > > > isTRUE(all.equal(length(x), 1)) > [1] TRUEPlease ignore that bit of rubbish above, clearly that is not what is required or wanted. Missed my afternoon coffee... G> 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 %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%