Full_Name: Axel Benz Version: 1.7.1 OS: Windows Submission from: (NULL) (137.251.33.43) This feature seems to be a basic bug:> 1=="1"[1] TRUE> as.numeric(1)=="1"[1] TRUE> as.numeric(1)==as.character("1")[1] TRUE isn't it necessary to distinguish beteen numbers and characters?? Best Regards, Axel
On Sat, 13 Sep 2003 13:37:28 +0200 (MET DST), you wrote:>Full_Name: Axel Benz >Version: 1.7.1 >OS: Windows >Submission from: (NULL) (137.251.33.43) > > >This feature seems to be a basic bug: > >> 1=="1" >[1] TRUE >> as.numeric(1)=="1" >[1] TRUE >> as.numeric(1)==as.character("1") >[1] TRUE > >isn't it necessary to distinguish beteen numbers and characters??No, the general rule in R is that operators try to coerce their arguments into a suitable type for the operation. In this case, because one arg is character, the other one is coerced to character too. It doesn't matter that you forced the type using as.numeric or as.character; the operator is going to force them both to comparable types. For example,> as.character(pi)[1] "3.14159265358979"> pi == "3.14159265358979"[1] TRUE> pi == 3.14159265358979[1] FALSE In the last case, they were already comparable, so no forcing was done. Since the internal value of pi actually carries more than 14 decimal places of precision, they don't compare equal as numbers, even though the do compare equal when converted to character. If you want to distinguish numbers and characters, use mode(1) =mode("1"). Duncan Murdoch
Dear Axel, At 01:37 PM 9/13/2003 +0200, axel.benz@iao.fhg.de wrote:>Full_Name: Axel Benz >Version: 1.7.1 >OS: Windows >Submission from: (NULL) (137.251.33.43) > > >This feature seems to be a basic bug: > > > 1=="1" >[1] TRUE > > as.numeric(1)=="1" >[1] TRUE > > as.numeric(1)==as.character("1") >[1] TRUE > >isn't it necessary to distinguish beteen numbers and characters??Coercion to a common mode takes place in many contexts; for example: > 1 & TRUE [1] TRUE To distinguish between numbers and characters you can, of course, use the predicates is.numeric() and is.character(), or test that the mode() of the arguments is the same. John ----------------------------------------------------- John Fox Department of Sociology McMaster University Hamilton, Ontario, Canada L8S 4M4 email: jfox@mcmaster.ca phone: 905-525-9140x23604 web: www.socsci.mcmaster.ca/jfox
axel.benz@iao.fhg.de writes:> Full_Name: Axel Benz > Version: 1.7.1 > OS: Windows > Submission from: (NULL) (137.251.33.43) > > > This feature seems to be a basic bug: > > > 1=="1" > [1] TRUE > > as.numeric(1)=="1" > [1] TRUE > > as.numeric(1)==as.character("1") > [1] TRUE > > isn't it necessary to distinguish beteen numbers and characters??As Martin said recently, if in doubt don't file bug reports. This may be strange, but it is S-compatible, even to the extent of cloning the following little piece of weirdness: S-PLUS : Copyright (c) 1988, 2000 MathSoft, Inc. S : Copyright Lucent Technologies, Inc. Version 6.0 Release 1 for Sun SPARC, SunOS 5.6 : 2000 Working data will be in /home/sfe/pd/MySwork> "1"==1[1] T> "1"==T[1] F> 1==T[1] T The rule is that there is implicit coercion in the direction that "always works", so the three cases above become "1" == as.character(1) "1" == as.character(T) 1 == as.numeric(T) There are various other curious consequences of that rule:> 1e-2=="0.01"[1] TRUE> 1e-2=="1e-2"[1] FALSE> "2"<10[1] FALSE> 2<"10"[1] FALSE but it *is* a feature and not a bug. -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk) FAX: (+45) 35327907
Have a look at the identical function:> identical(1,"1")[1] FALSE> identical(as.numeric(1),"1")[1] FALSE> identical(as.numeric(1),as.character("1"))[1] FALSE> identical(1,1)[1] TRUE --- axel.benz@iao.fhg.de wrote:>Full_Name: Axel Benz >Version: 1.7.1 >OS: Windows >Submission from: (NULL) (137.251.33.43) > > >This feature seems to be a basic bug: > >> 1=="1" >[1] TRUE >> as.numeric(1)=="1" >[1] TRUE >> as.numeric(1)==as.character("1") >[1] TRUE > >isn't it necessary to distinguish beteen numbers and characters?? > >Best Regards, >Axel > >______________________________________________ >R-devel@stat.math.ethz.ch mailing list >https://www.stat.math.ethz.ch/mailman/listinfo/r-devel
Hi Axel, You may wish to consult the man page for the '==' operator and the 'identical' function.> identical(1, "1")[1] FALSE> ? "==" > ? identicalAnd probably your notion of a "bug" is too wide: please have a look at the section "R Bugs", 9.1 in the R FAQ. Best regards Wolfgang ------------------------------------- Wolfgang Huber Division of Molecular Genome Analysis German Cancer Research Center Heidelberg, Germany Phone: +49 6221 424709 Fax: +49 6221 42524709 Http: www.dkfz.de/mga/whuber ------------------------------------- On Sat, 13 Sep 2003 axel.benz@iao.fhg.de wrote:> Full_Name: Axel Benz > Version: 1.7.1 > OS: Windows > Submission from: (NULL) (137.251.33.43) > > > This feature seems to be a basic bug: > > > 1=="1" > [1] TRUE > > as.numeric(1)=="1" > [1] TRUE > > as.numeric(1)==as.character("1") > [1] TRUE > > isn't it necessary to distinguish beteen numbers and characters?? > > Best Regards, > Axel > > ______________________________________________ > R-devel@stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-devel >