[R] ignorable warnings of as.numeric: NAs introduced by coercion

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Oct 17 10:57:49 CEST 2007


On Wed, 17 Oct 2007, Wolfram Fischer wrote:

> Warnings are a good thing and can help to find errors in data.
> But with newer R versions I get more and more warnings
> which I would like to suppress in defined circumstances.

This warning happened in R 1.8.1, for example: it is not new.  I don't 
think R is giving 'more and more warnings': perhaps it is your usage of R 
that has changed.

> In the manual of as.numeric() is noticed:
> 	as.numeric(c("-.1"," 2.7 ","B")) # (-0.1, 2.7, NA)  +  warning
>
> If I know that my data contains characters and if I would like
> that they will be converted to NA: how I can turn off just this warning?
>
> (I know I could do: options(warn=-1). But then I will miss all
> other possibly necessary warnings.)
>
> I would like to have an argument to as.numeric() like na.coerce=TRUE.
> It would be especially helpful when converting factors to numbers by doing:
> 	as.numeric( levels( x ) )[ x ]
>
> What could I do?

Your homework!

help.search("suppress warnings") gets you to the correct help page. E.g.

> suppressWarnings(as.numeric(c("-.1"," 2.7 ","B")))
[1] -0.1  2.7   NA

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list