[R] Condition to factor (easy to remember)

Dieter Menne dieter.menne at menne-biomed.de
Wed Sep 30 09:43:13 CEST 2009


Dear List,

creating factors in a given non-default orders is notoriously difficult to
explain in a course. Students love the ifelse construct given below most,
but I remember some comment from Martin Mächler (?) that ifelse should be
banned from courses.

Any better idea? Not necessarily short, easy to remember is important.

Dieter


data = c(1,7,10,50,70)
levs = c("Pre","Post")

# Typical C-Programmer style
factor(levs[as.integer(data >10)+1], levels=levs)

# Easiest to understand
factor(ifelse(data <=10, levs[1], levs[2]), levels=levs)

-- 
View this message in context: http://www.nabble.com/Condition-to-factor-%28easy-to-remember%29-tp25676411p25676411.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list