Jonathan Dushoff
2012-Nov-15 19:52 UTC
[R] Selecting the "non-attribute" part of an object
I have two matrices, generated by R functions that I don't understand. I want to confirm that they're the same, but I know that they have different attributes. If I want to compare the dimnames, I can say> identical(attr(tm, "dimnames"), attr(tmm, "dimnames"))[1] FALSE or even:> identical(dimnames(tm), dimnames(tmm))[1] FALSE But I can't find any good way to compare the "main" part of objects. What I'm doing now is:> tm_new <- tm > tmm_new <- tmm> attributes(tm_new) <- attributes(tmm_new) <- NULL> identical(tm_new, tmm_new)[1] TRUE But that seems very inaesthetic, besides requiring that I create two pointless objects. I have read ?attributes, ?attr and some web introductions to how R objects work, but have not found an answer. Thanks for any help.
max(abs(x-y)) < numerical tolerance of your choice -- Bert On Thu, Nov 15, 2012 at 11:52 AM, Jonathan Dushoff <dushoff at mcmaster.ca> wrote:> I have two matrices, generated by R functions that I don't understand. > I want to confirm that they're the same, but I know that they have > different attributes. > > If I want to compare the dimnames, I can say > >> identical(attr(tm, "dimnames"), attr(tmm, "dimnames")) > [1] FALSE > > or even: > >> identical(dimnames(tm), dimnames(tmm)) > [1] FALSE > > But I can't find any good way to compare the "main" part of objects. > > What I'm doing now is: > >> tm_new <- tm >> tmm_new <- tmm > >> attributes(tm_new) <- attributes(tmm_new) <- NULL > >> identical(tm_new, tmm_new) > [1] TRUE > > But that seems very inaesthetic, besides requiring that I create two > pointless objects. > > I have read ?attributes, ?attr and some web introductions to how R > objects work, but have not found an answer. > > Thanks for any help. > > ______________________________________________ > 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.-- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
all.equal() will give some details on the differences between your objects. If you don't care that some attribute will differ, either ignore all.equal's output concerning it or remove it before giving the object to all.equal. E.g., if you don't care about dimnames but do care about dimensions do canonicalize <- function(x) structure(x, dimnames=NULL) all.equal(canonicalize(tm), canonicalize(tmm)) Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf > Of Jonathan Dushoff > Sent: Thursday, November 15, 2012 11:53 AM > To: r-help at r-project.org > Subject: [R] Selecting the "non-attribute" part of an object > > I have two matrices, generated by R functions that I don't understand. > I want to confirm that they're the same, but I know that they have > different attributes. > > If I want to compare the dimnames, I can say > > > identical(attr(tm, "dimnames"), attr(tmm, "dimnames")) > [1] FALSE > > or even: > > > identical(dimnames(tm), dimnames(tmm)) > [1] FALSE > > But I can't find any good way to compare the "main" part of objects. > > What I'm doing now is: > > > tm_new <- tm > > tmm_new <- tmm > > > attributes(tm_new) <- attributes(tmm_new) <- NULL > > > identical(tm_new, tmm_new) > [1] TRUE > > But that seems very inaesthetic, besides requiring that I create two > pointless objects. > > I have read ?attributes, ?attr and some web introductions to how R > objects work, but have not found an answer. > > Thanks for any help. > > ______________________________________________ > 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.
I think that what you are looking for is: all.equal(tm,tmm,check.attributes=FALSE) But BEWARE: m <- matrix(1:36,4,9) mm <- matrix(1:36,12,3) all.equal(m,mm,check.attributes=FALSE) gives TRUE!!! I.e. sometimes attributes really are vital characteristics. cheers, Rolf Turner On 16/11/12 08:52, Jonathan Dushoff wrote:> I have two matrices, generated by R functions that I don't understand. > I want to confirm that they're the same, but I know that they have > different attributes. > > If I want to compare the dimnames, I can say > >> identical(attr(tm, "dimnames"), attr(tmm, "dimnames")) > [1] FALSE > > or even: > >> identical(dimnames(tm), dimnames(tmm)) > [1] FALSE > > But I can't find any good way to compare the "main" part of objects. > > What I'm doing now is: > >> tm_new <- tm >> tmm_new <- tmm >> attributes(tm_new) <- attributes(tmm_new) <- NULL >> identical(tm_new, tmm_new) > [1] TRUE > > But that seems very inaesthetic, besides requiring that I create two > pointless objects. > > I have read ?attributes, ?attr and some web introductions to how R > objects work, but have not found an answer. > > Thanks for any help. > > ______________________________________________ > 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. >
Jonathan Dushoff
2012-Nov-15 23:59 UTC
[R] Selecting the "non-attribute" part of an object
Thanks for all of these useful answers. Thanks also to Ben Bolker, who told me offline that c() is a general way to access the "main" part of an object (not tested). I also tried:> identical(matrix(tm), matrix(tmm))[1] TRUE which also works, but does _not_ solve the problem Rolf warns about below (to my disappointment). JD On Thu, Nov 15, 2012 at 6:47 PM, Rolf Turner <rolf.turner at xtra.co.nz> wrote:> I think that what you are looking for is:> all.equal(tm,tmm,check.attributes=FALSE)> But BEWARE:> m <- matrix(1:36,4,9) > mm <- matrix(1:36,12,3) > all.equal(m,mm,check.attributes=FALSE)> gives TRUE!!! I.e. sometimes attributes really are vital characteristics.> cheers,> Rolf Turner> On 16/11/12 08:52, Jonathan Dushoff wrote:>> I have two matrices, generated by R functions that I don't understand. >> I want to confirm that they're the same, but I know that they have >> different attributes.>> If I want to compare the dimnames, I can say>>> identical(attr(tm, "dimnames"), attr(tmm, "dimnames"))>> [1] FALSE>> or even:>>> identical(dimnames(tm), dimnames(tmm))>> [1] FALSE>> But I can't find any good way to compare the "main" part of objects.>> What I'm doing now is:>>> tm_new <- tm >>> tmm_new <- tmm >>> attributes(tm_new) <- attributes(tmm_new) <- NULL >>> identical(tm_new, tmm_new)>> [1] TRUE>> But that seems very inaesthetic, besides requiring that I create two >> pointless objects.>> I have read ?attributes, ?attr and some web introductions to how R >> objects work, but have not found an answer.>> Thanks for any help.>> ______________________________________________ >> 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.