Ramnik Bansal
2016-Nov-21 09:52 UTC
[R] explicit coercion warnings as.numeric Versus as.logical
Hi, I am trying to understand under which specific conditions does explicit coercion produce warnings.> as.numeric(c(1, F, "b"))[1] 1 NA NA Warning message: NAs introduced by coercion> as.logical(c(1, F, "b"))[1] 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. Ramnik [[alternative HTML version deleted]]
Bert Gunter
2016-Nov-21 15:57 UTC
[R] explicit coercion warnings as.numeric Versus as.logical
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:> Hi, > > I am trying to understand under which specific conditions does explicit > coercion produce warnings. > >> as.numeric(c(1, F, "b")) > [1] 1 NA NA > Warning message: > NAs introduced by coercion > >> as.logical(c(1, F, "b")) > [1] 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. > Ramnik > > [[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.
Jim Lemon
2016-Nov-21 21:35 UTC
[R] explicit coercion warnings as.numeric Versus as.logical
Hi Ramnik, Bert's answer is correct, and an easy way to see why is to look at: c(1,F,"b") [1] "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: F<-"foo" c(1,F,"b") [1] "1" "foo" "b" as.logical(c(1,F,"b")) [1] 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 conditional like: x != 0 Jim 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: >> Hi, >> >> I am trying to understand under which specific conditions does explicit >> coercion produce warnings. >> >>> as.numeric(c(1, F, "b")) >> [1] 1 NA NA >> Warning message: >> NAs introduced by coercion >> >>> as.logical(c(1, F, "b")) >> [1] 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. >> Ramnik >> >> [[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. > > ______________________________________________ > 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.