Re: [R] Factoring a variable

From: Steve Lianoglou <>
Date: Thu, 17 Jun 2010 16:57:36 -0400


On Thu, Jun 17, 2010 at 3:45 PM, Noah Silverman <> wrote:
> Hi,
> I have a dataset where the results are coded ("yes", "no")  We want to
> do some machine learning with SVM to predict the "yes" outcome
> My problem is that if I just use the as.factor function to convert, then
> it reverses the levels.
> ----------------------
> x <- c("no", "no", "no", "yes", "yes", "no", "no")
>  as.factor(x)
> [1] no  no  no  yes yes no  no
> Levels: no yes
> ----------------------
> The SVM function (in the e1071 package) sees "no" as the first label and
> treats that as the positive outcome.
> The problem arises when we look at the decision values of the
> predictions.  Everything is gauged as values for "no".
> So, is there a way to force R to use my specified order when converting
> to factors?
> I've tried as.factor(x, levels=c("yes", "no")) but that throws errors
> about unused arguments.
> Any help?

Yes, look at the error message you're getting in your call to `as.factor` more closely -- also look at the help for as.factor and note there is no "levels" argument:

R> x <- c("no", "no", "no", "yes", "yes", "no", "no") R> factor(x, levels=c('yes', 'no'))
[1] no no no yes yes no no
Levels: yes no


Steve Lianoglou
Graduate Student: Computational Systems Biology
 | Memorial Sloan-Kettering Cancer Center
 | Weill Medical College of Cornell University
Contact Info:

______________________________________________ mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.
Received on Thu 17 Jun 2010 - 20:59:57 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 17 Jun 2010 - 21:00:32 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive