Heinz Tuechler
2008-Nov-09 12:24 UTC
[R] attr.all.equal() and all.equal(attributes(), attributes())
Dear All! If I try to compare the attributes of two objects, I find a surprising behaviour of attr.all.equal(). With identical attributes I receive the answert NULL. If the attributes differ, the answer is as expecxted and differences are shown. all.equal(attributes(), attributes()) instead returns TRUE, if attributes are equal. See example: v <- 1:5 attr(v, 'testattribute') <- 'testattribute v' v_c <- v attributes(v) $testattribute [1] "testattribute v" attributes(v_c) $testattribute [1] "testattribute v" all.equal(v, v_c) [1] TRUE attr.all.equal(v, v_c) NULL << - - - - - - - - - - here is, what I don't expected all.equal(attributes(v), attributes(v_c)) [1] TRUE attr(v_c, 'testattribute') <- 'testattribute v_c' attr.all.equal(v, v_c) [1] "Attributes: < Component 1: 1 string mismatch >" all.equal(attributes(v), attributes(v_c)) [1] "Component 1: 1 string mismatch" Thanks for your attention Heinz T?chler Version: platform = i386-pc-mingw32 arch = i386 os = mingw32 system = i386, mingw32 status = Patched major = 2 minor = 8.0 year = 2008 month = 11 day = 04 svn rev = 46830 language = R version.string = R version 2.8.0 Patched (2008-11-04 r46830) Windows XP (build 2600) Service Pack 2 Locale: LC_COLLATE=German_Austria.1252;LC_CTYPE=German_Austria.1252;LC_MONETARY=German_Austria.1252;LC_NUMERIC=C;LC_TIME=German_Austria.1252 Search Path: .GlobalEnv, package:stats, package:graphics, package:grDevices, package:utils, package:datasets, package:methods, Autoloads, package:base
Peter Dalgaard
2008-Nov-09 13:24 UTC
[R] attr.all.equal() and all.equal(attributes(), attributes())
Heinz Tuechler wrote:> Dear All! > > If I try to compare the attributes of two objects, I find a surprising > behaviour of attr.all.equal(). With identical attributes I receive the > answert NULL. If the attributes differ, the answer is as expecxted and > differences are shown. > all.equal(attributes(), attributes()) instead returns TRUE, if > attributes are equal. >That _is_ as documented, although if you want to quibble, it should probably also have been mentioned in the Value section. I don't know if there's a a rationale for it. The actual code goes msg <- NULL if (....) msg <- c(msg, some text) if (..... msg so it returns NULL if none of the ifs are taken, but it could easily be changed to return if (is.null(msg)) TRUE else msg ("easily" depending, of course, on how much code actually depends on the current behaviour...)> See example: > > v <- 1:5 > attr(v, 'testattribute') <- 'testattribute v' > v_c <- v > attributes(v) > $testattribute > [1] "testattribute v" > > attributes(v_c) > $testattribute > [1] "testattribute v" > > all.equal(v, v_c) > [1] TRUE > attr.all.equal(v, v_c) > NULL << - - - - - - - - - - here is, what I don't expected > all.equal(attributes(v), attributes(v_c)) > [1] TRUE > > attr(v_c, 'testattribute') <- 'testattribute v_c' > attr.all.equal(v, v_c) > [1] "Attributes: < Component 1: 1 string mismatch >" > all.equal(attributes(v), attributes(v_c)) > [1] "Component 1: 1 string mismatch" > > Thanks for your attention > > Heinz T?chler > > Version: > platform = i386-pc-mingw32 > arch = i386 > os = mingw32 > system = i386, mingw32 > status = Patched > major = 2 > minor = 8.0 > year = 2008 > month = 11 > day = 04 > svn rev = 46830 > language = R > version.string = R version 2.8.0 Patched (2008-11-04 r46830) > > Windows XP (build 2600) Service Pack 2 > > Locale: > LC_COLLATE=German_Austria.1252;LC_CTYPE=German_Austria.1252;LC_MONETARY=German_Austria.1252;LC_NUMERIC=C;LC_TIME=German_Austria.1252 > > > Search Path: > .GlobalEnv, package:stats, package:graphics, package:grDevices, > package:utils, package:datasets, package:methods, Autoloads, package:base > > ______________________________________________ > 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.-- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907