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 [[alternative HTML version deleted]]
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 understandthe only> way is to transform it into a factor variable. > > Is there a way to check that the numeric values of the new factorvariable> 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
On 19/09/2014, 6:53 AM, 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?If you apply as.numeric() to a factor, you won't get a zero value. Internal factor values start at 1. So I wouldn't rely on the internal storage to achieve whatever it is you want to achieve. Use explicit computation, e.g. words <- ifelse(var == 0, "Bad", ifelse(var == 1, "Good", NA)) values <- ifelse(words == "Bad", 0, ifelse(words == "Good", 1, NA)) Duncan Murdoch