[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: ct...@york.ac.uk __ R-help@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.
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????
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 __ R-help@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.
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????
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 __ R-help@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.
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, 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 %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% __ R-help@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.
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, 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 %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% __ R-help@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.
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, 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] TRUE Please 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 %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% __ R-help@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.
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 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? 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: ct...@york.ac.uk __ R-help@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.
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 tlum...@u.washington.eduUniversity of Washington, Seattle __ R-help@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.
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 Sep 15, 2009, at 1:04 PM, 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) identical() tests for the two objects having the same class for one thing: class(length(c(9)) ) [1] integer class(1) [1] numeric class(1) != class( length( c(9) ) ) [1] TRUE David Winsemius, MD Heritage Laboratories West Hartford, CT __ R-help@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.
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????
Corrado writes: 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? identical() returns FALSE because they are not identical, even if they are the same value. length() returns an integer type, while 1 is type double. If you want them to be identical, use this: identical(length(x),as.integer(1)) cur -- Curt Seeliger, Data Ranger Raytheon Information Services - Contractor to ORD seeliger.c...@epa.gov 541/754-4638 [[alternative HTML version deleted]] __ R-help@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.
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, 2009-09-15 at 18:04 +0100, 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 So why don't you put at the start of you code so all we have to do is paste it in? I grabbed the code pasted it in without thinking and it failed, only then did I really look at it. At that stage you might loose people who are busy. Unfortunately for you (and others) those people tend to be one the ones that know R the best and give the most insightful answers. What is the logic behind identical(length(x),1) being false? I and the other responders showed you that in R, 1 is a double and thus is different to the object returned from length(x), which is an integer. identical tests for exact equality of /objects/. An integer is not a double. Are you confusing the object with the contents of said object? Even then they are different for the reason mentioned typeof(1) [1] double typeof(1L) [1] integer typeof(length(1)) [1] integer identical is comparing the objects. 1 is and orange, length(x) returns an apple. They are different despite the fact that the innards are both nice, sweet and juicy. This is what identical is telling you. If you want to know whether length(1) (i.e. 1) is equal to 1 then you can use == or make your comparison in identical exact and cast 1 and an integer using 1L. My bit of rubbish I posted may not be so rubbish here if your real use case involves any computations, for example in a situation where isTRUE(all.equal(length(x), Y)) where Y is the result of some other computation. 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 %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% __ R-help@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.
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????
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-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Thomas Lumley Sent: Tuesday, September 15, 2009 10:12 AM To: Corrado Cc: r-help@r-project.org; gavin.simp...@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 tlum...@u.washington.edu University of Washington, Seattle __ R-help@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. __ R-help@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.