# [R] problems with coercing a factor to be numeric

William Dunlap wdunlap at tibco.com
Wed Jan 23 17:56:04 CET 2013

```To find the proportion of "yes"s in pp you can use
mean(pp == "yes")
and avoid the conversion of a factor to integer (and
subtracting 1).  The above works for character and factor
pp.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of Francesco Sarracino
> Sent: Wednesday, January 23, 2013 1:59 AM
> To: D. Rizopoulos
> Cc: R help
> Subject: Re: [R] problems with coercing a factor to be numeric
>
> Thanks,
> this works! but I am surprised that R has such a strange behavior and that
> there is no way to control it.
> BTW, also as.integer(pp)-1 works!
> Still, it doesn't look to me as a first best.
> At any rate, thanks a lot for your help.
> f.
>
>
> On 23 January 2013 10:53, D. Rizopoulos <d.rizopoulos at erasmusmc.nl> wrote:
>
> > check also
> >
> > pp <- rep(0:1, 10)
> > pp <- factor(pp, levels=(0:1), labels=c("no","yes"))
> >
> > unclass(pp)
> > unclass(pp) - 1
> >
> >
> > Best,
> > Dimitris
> >
> >
> > On 1/23/2013 10:48 AM, Francesco Sarracino wrote:
> > > Dear Dimitris,
> > >
> > > thanks for your quick reply. I've tried the solutions proposed in 7.10
> > > How do I convert factors to numeric?
> > >
> > > as.numeric(as.character(pp))
> > > and
> > > as.numeric(levels(pp))[as.integer(pp)]
> > >
> > > However, whatever I do, I get "Warning message: NAs introduced by
> > coercion"
> > > and the output is a vector of NA.
> > >
> > > Any ideas?
> > > f.
> > >
> > >
> > >
> > > On 23 January 2013 10:39, D. Rizopoulos <d.rizopoulos at erasmusmc.nl
> > > <mailto:d.rizopoulos at erasmusmc.nl>> wrote:
> > >
> > >     Check R FAQ 7.10: How do I convert factors to numeric?
> > >
> > >
> > >     I hope it helps.
> > >
> > >     Best,
> > >     Dimitris
> > >
> > >
> > >     On 1/23/2013 10:33 AM, Francesco Sarracino wrote:
> > >      > Dear R listers,
> > >      >
> > >      > I am trying to compute the mean of a dummy variable that is
> > >     encoded as a
> > >      > factor. However, even though the levels of my factor are 0 - 1,
> > >     when I
> > >      > compute the mean (after coercing the factor to be
> > >      > numeric), R changes 0 into 1 and 1 into yes, thus altering my
> > >     expected
> > >      > result.
> > >      >
> > >      > Please, consider the following working example:
> > >      > pp <- rep(0:1, 10)
> > >      > pp <- factor(pp, levels=(0:1), labels=c("no","yes"))
> > >      > mean(pp) #this won't work because the argument is not numeric or
> > >     logical
> > >      > mean(as.integer(pp)) # this computes the average, but not on the
> > >     range 0-1,
> > >      > but 1-2. Indeed, the result is 1.5 and not 0.5 as expected.
> > >      >
> > >      > What am I doing wrong?
> > >      > f.
> > >      >
> > >      >
> > >
> > >     --
> > >     Dimitris Rizopoulos
> > >     Assistant Professor
> > >     Department of Biostatistics
> > >     Erasmus University Medical Center
> > >
> > >     Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
> > >     Tel: +31/(0)10/7043478 <tel:%2B31%2F%280%2910%2F7043478>
> > >     Fax: +31/(0)10/7043014 <tel:%2B31%2F%280%2910%2F7043014>
> > >     Web: http://www.erasmusmc.nl/biostatistiek/
> > >
> > >
> > >
> > >
> > > --
> > > Francesco Sarracino, Ph.D.
> >
> > --
> > Dimitris Rizopoulos
> > Assistant Professor
> > Department of Biostatistics
> > Erasmus University Medical Center
> >
> > Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
> > Tel: +31/(0)10/7043478
> > Fax: +31/(0)10/7043014
> > Web: http://www.erasmusmc.nl/biostatistiek/
> >
>
>
>
> --
> Francesco Sarracino, Ph.D.