[R] "--" < 0
@vi@e@gross m@iii@g oii gm@ii@com
@vi@e@gross m@iii@g oii gm@ii@com
Tue Jun 25 16:42:42 CEST 2024
Unfortunately, Adrian, even trying to coerce a string like "--" to numeric just produces an NA and comparison fails:
> as.numeric("--") < 0
[1] NA
Warning message:
NAs introduced by coercion
The same is true of anything that is character as it cannot be coerced.
> as.numeric("anything") < 0
[1] NA
Warning message:
NAs introduced by coercion
So, the only rational thing R can do is convert the zero to a character string and that has the usual problem as in:
> as.character(20) < as.character(9)
[1] TRUE
Can you suggest if there is any valid result in some cases like these? I mean, if we wrote a function like greater(first, second) and set it up so it analyzed the types of the arguments and tried various combinations of converting things to one of several possible types such as integer/numeric/character/logical/raw and examined the results of each while catching errors, what would be a right answer it could get?
My guess is that it may not be possible in some cases to give an answer other than that the two being compared are incompatible. The result of greater() could be neither TRUE, nor FALSE but something like one of the NA family or maybe a NaN or maybe the function would simply throw an error and fail.
I can picture many other scenarios such as asking it to compare two objects, perhaps the same type or different types, such as a list versus a matrix. Realistically, the ">" operator and other such operators are not designed to do deep analyses and be able to compare complex-objects component by component and recursively.
So the result you show, is in some sense nonsense. It is so wrong that it is not even wrong. My intuition is that it is similar to this:
> NA + 5
[1] NA
Any computation containing a crucial NA, typically ends up with an NA result because once you introduce uncertainty, it propagates. So the result of comparing things that cannot be compared may well also be polluted and should come back as an NA, probably the default logical form, not something like NA_integer_
Since comparisons between random objects are something that can happen, I suspect there are functions already part of the R distribution or in some packages that behave better and can safely be used to ask if things can even be compared. But there may yet remain edge cases because of things like character strings that can be evaluated as numeric but may not be evaluated correctly if they are written in some way as in hexadecimal notation or scientific notation.
-----Original Message-----
From: R-help <r-help-bounces using r-project.org> On Behalf Of Adrian Dusa
Sent: Tuesday, June 25, 2024 4:59 AM
To: Martin Maechler <maechler using stat.math.ethz.ch>
Cc: r-help using r-project.org
Subject: Re: [R] "--" < 0
Oh I see...
It's not that "-7" gets coerced to numeric, but 0 gets coerced to "0".
Of course...
On Tue, Jun 25, 2024 at 11:02 AM Martin Maechler <maechler using stat.math.ethz.ch>
wrote:
> >>>>> Adrian Dusa
> >>>>> on Tue, 25 Jun 2024 10:56:07 +0300 writes:
>
> > Dear R fellows,
>
> >> From time to time, just when I thought I knew my R, I get
> >> bitten by some
> > small things that reminds one to constantly return to the
> > basics.
>
> > I knew for instance that "-1" < 0 is TRUE, presumably
> > because R first coerces to numeric before comparing with
> > 0.
>
> > But I did not expect that "--" < 0 is a TRUE statement.
> > (and the same holds for any string prepended by a minus
> > sign, e.g. "-a" < 0)
>
> > I would be grateful for an explanation, I'm sure that
> > something very obvious escapes me but it sure does seem
> > counter intuitive to me.
>
> > Best wishes, Adrian
>
> > [[alternative HTML version deleted]]
>
> Nice, quiz, yes.
>
> You must have forgotten that all Op's (+,-, <= , &, | ..)
> must coerce to common type.
>
> ... and so does c() where coercion is defined a bit more.
>
> --> does c("--", 0) give you a clue, now ?
>
[[alternative HTML version deleted]]
______________________________________________
R-help using 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.
More information about the R-help
mailing list