[R] NA treatment when comparing two vectors
fabien.verger at gmail.com
Tue Oct 4 22:08:31 CEST 2016
Thank you all - Bert, Marc and William - for your suggestions and the
material (Rseek, r4stats.com...).
Following your advices, I will create a dedicated function since I will
have to use this tool in many programs.
Actually, I regularly load economic data from various sources and I need to
spot what is *different* than the previous version, and what is *new*,
hence the utility of this function and this special NA treatment.
Thanks again for your help and for your reactivity.
2016-10-04 21:17 GMT+02:00 Marc Schwartz <marc_schwartz at me.com>:
> I had initially thought of using all.equal() along with isTrue(), however
> Fabien's desire for a pairwise element based approach, along with the
> specific NA handling steered me away from it and towards code along the
> lines of what Bert had offered, which builds on the basic notion of
> approximate numeric equality as used in all.equal().
> > On Oct 4, 2016, at 2:02 PM, William Dunlap via R-help <
> r-help at r-project.org> wrote:
> > The all.equal function returns TRUE if two objects are pretty similar to
> > one another and a textual description of the differences if not. Use
> > isTRUE on its output if you just want a TRUE/FALSE answer.
> >> all.equal(c(1,2*(1+.Machine$double.eps),NaN), c(1,2,NaN))
> >  TRUE
> >> all.equal(c(1,2*(1+.Machine$double.eps),NaN), c(1,2,NaN), tolerance=0)
> >  "Mean relative difference: 2.220446e-16"
> >> isTRUE(all.equal(c(1,2*(1+.Machine$double.eps),NaN), c(1,2,NaN),
> > tolerance=0))
> >  FALSE
> >> all.equal(c(a=1, b=2), c(x=1, b=2))
> >  "Names: 1 string mismatch"
> > Bill Dunlap
> > TIBCO Software
> > wdunlap tibco.com
> > On Tue, Oct 4, 2016 at 9:51 AM, fabien verger <fabien.verger at gmail.com>
> > wrote:
> >> Hello,
> >> I want to get the differences when comparing 2 vectors, by pair
> (element by
> >> element).
> >> I'd like to get TRUEs when:
> >> - the two compared elements are different and non-missing (like `!=`
> >> - one element is missing and the other is not missing (unfortunatelly
> >> gives NA and not TRUE)
> >> Note that I don't want to get TRUEs when both are missing. NA or FALSE
> >> fine.
> >> Given a and b:
> >>> a <- c(1, 2, 3, NA, NA)
> >>> b <- c(1, 9, NA, 4 , NA)
> >> The only solution I found is:
> >>> a != b | (is.na(a) != is.na(b))
> >>  FALSE TRUE TRUE TRUE NA
> >> Is there a single function which can do the same?
> >> I searched for other comparison tools but found nothing relevant.
> >> And I would like also to avoid using `!=` because I'm often comparing
> >> floating numbers computed by different algorithms (so rounded
> >> I found identical() interesting (for exemple, !(identical(NA, 99)) gives
> >> TRUE) but the result of !(identical(a, b) is a single logical, not a
> >> of logicals.
> >> Many thanks in advance for your help.
> >> P.S. I am new to R, coming from SAS. Actually, I'm looking for the R
> >> function that replicates the SAS instruction: if a ^= b;
> >> [[alternative HTML version deleted]]
> >> ______________________________________________
> >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> >> 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.
> > [[alternative HTML version deleted]]
> > ______________________________________________
> > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/
> > and provide commented, minimal, self-contained, reproducible code.
[[alternative HTML version deleted]]
More information about the R-help