[R] Help: problem converting character to numeric

Petr Savicky savicky at cs.cas.cz
Wed Mar 14 23:26:40 CET 2012


On Wed, Mar 14, 2012 at 03:07:19PM -0700, Aurélien PHILIPPOT wrote:
> Dear R experts,
> I have a dataframe imported from a csv file (with read.csv).
> 
> Here is an example:
> 
> yyyymm<- c("19860228", "19860331","19860430","19860531")
> id<-c("10000","10000","10000","10000")
> re<- c("C","0.25", "0.98", "1.34")
> 
> mret<-data.frame(yyyymm, id, re)
> 
> mret<-as.numeric(as.character(mret$re))
> Error: (converted from warning) NAs introduced by coercion
> 
> 
> One of the column ("re" in the example above) has characters and numbers,
> but it should be treated a numeric column. Therefore, I want to eliminate
> the rows in which the variable re has characters values (the first row in
> the example).
> In the past, I always used this code successfully (the characters were
> replaced by NA, and only a warning message was issued). But now, I have an
> error message and it no longer works. Could anyone suggest an alternative
> way to do it?

Hi.

You probably have options(warn=2) set. This may be checked
using  getOption("warn"). If you set options(warn=0), then
the warning will not be converted to an error.

Hope this helps.

Petr Savicky.



More information about the R-help mailing list