[R] Factoring a variable

Erik Iverson eriki at ccbr.umn.edu
Thu Jun 17 21:53:32 CEST 2010



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

You want to use ?factor, not ?as.factor.  Then you can use the "levels" 
argument.

factor(x, levels = c("yes", "no"))

Also see ?relevel.



More information about the R-help mailing list