Thursday, April 7, 2011

Recoding Variables in R: An Example

Recoding is pretty easy in R. You can use a control structure such as an if statement for binary factors e.g.

mnew$Assignable <- ifelse(mnew$Assignable == T, c("Assignee"), c("Non-assignee"))

 In this example I am replacing True False values with "Assignee", "Non-assignee". T is shorthand for TRUE which is an R type. The messy part about this is the c() vector that you have to create as a wrapper around the new factor value.

The computer scientist in me wants to keep the boolean values in there but whenever I try to do a graph or a table using this column of the data frame I end up with FALSE or TRUE as a label or a column heading.

0 comments:

Post a Comment