[R] explicit coercion warnings as.numeric Versus as.logical
drjimlemon at gmail.com
Mon Nov 21 22:35:44 CET 2016
Bert's answer is correct, and an easy way to see why is to look at:
 "1" "FALSE" "b"
The reason that "F" is translated to "FALSE" is that is its default
value when R is started. If you change that value:
 "1" "foo" "b"
 NA NA NA
To find out why as.numeric warns you and as.logical doesn't, we will
have to await the response of someone who knows. I suspect that
as.numeric employs a fairly sophisticated analysis of a string to see
if it is a number while as.logical just shoves the value (x) into a
x != 0
On Tue, Nov 22, 2016 at 2:57 AM, Bert Gunter <bgunter.4567 at gmail.com> wrote:
> Not an answer, but note that your vectors are all first (silently)
> coerced to character, as vectors must be all of one type.
> I would hazard a guess that the answer is: it's simply an arbitrary
> inconsistency (different folks wrote the functions at different
> times). Note that AFAICS, the difference has no effect on the behavior
> of the two functions, i.e. the behavior is consistent, which is what
> counts. However, I of course defer to real experts.
> -- Bert
> Bert Gunter
> "The trouble with having an open mind is that people keep coming along
> and sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
> On Mon, Nov 21, 2016 at 1:52 AM, Ramnik Bansal <ramnik.bansal at gmail.com> wrote:
>> I am trying to understand under which specific conditions does explicit
>> coercion produce warnings.
>>> as.numeric(c(1, F, "b"))
>>  1 NA NA
>> Warning message:
>> NAs introduced by coercion
>>> as.logical(c(1, F, "b"))
>>  NA FALSE NA
>> In above examples, as.numeric produces warning but as.logical does not.
>> What is the reason behind this different behaviour. Ideally as.logical
>> should also have produced the warning message like as.numeric.
>> Thanks in advance.
>> [[alternative HTML version deleted]]
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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