[R] See the numeric codes of a factor

Jim Lemon jim at bitwrit.com.au
Fri Sep 19 13:31:13 CEST 2014


On Fri, 19 Sep 2014 12:53:16 PM Angel Rodriguez wrote:
> Dear Subscribers,
> 
> I want to label a numeric variable 0="Bad" /1="Good". I understand 
the only
> way is to transform it into a factor variable.
> 
> Is there a way to check that the numeric values of the new factor 
variable
> are 0 and 1 and not 1 and 2?
> 
> Thank you very much.
> 
> Angel Rodriguez-Laso
> 
Hi Angel,
As far as I know, the numeric values of factors always begin with 1. If your 
factor (badgood) is constructed from the values "Bad" and "Good":

as.numeric(badgood) - 1

will produce a vector of zeros and ones.

If you have a numeric vector of zeros and ones and want the 
corresponding vector of "Bad" and "Good":

ifelse(badgood,"Good","Bad")

Jim



More information about the R-help mailing list