Aurélien PHILIPPOT
2012-Mar-14 22:07 UTC
[R] Help: problem converting character to numeric
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?
Thanks a lot,
Best,
Aurelien
[[alternative HTML version deleted]]
Dear Aurelien,
Thanks for the reproducible example. Here is one way:
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)
subset(mret, !is.na(as.numeric(as.character(re))))
HTH,
Jorge.-
On Wed, Mar 14, 2012 at 6:07 PM, 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?
>
> Thanks a lot,
> Best,
> Aurelien
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@r-project.org mailing list
> 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.
>
[[alternative HTML version deleted]]
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.
Gabor Grothendieck
2012-Mar-14 22:32 UTC
[R] Help: problem converting character to numeric
On Wed, Mar 14, 2012 at 6:07 PM, Aur?lien PHILIPPOT <aurelien.philippot at gmail.com> 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 >Check the value of the warn option:> getOption("warn")[1] 0> as.numeric("C") # ok just gives warning[1] NA Warning message: NAs introduced by coercion> options(warn = 2) > as.numeric("C") # now its an errorError: (converted from warning) NAs introduced by coercion -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com