[R] Condition to factor (easy to remember)

hadley wickham h.wickham at gmail.com
Wed Sep 30 21:47:58 CEST 2009


On Wed, Sep 30, 2009 at 2:32 PM, Ista Zahn <istazahn at gmail.com> wrote:
> An extremely verbose, but (in my view) easy to understand approach is:
>
>> data.f <- data; data.f[which(data <= 10)] <- levs[1]; data.f[which(data > 10)] <- levs[2]; data.f <- factor(data.f)
>

All those which()s are unnecessary.  And if you're going to use this
approach I'd recommend initialising data.f with NA's so you can tell
if you missed any cases.

Hadley


-- 
http://had.co.nz/




More information about the R-help mailing list