[R] Condition to factor (easy to remember)

Ista Zahn istazahn at gmail.com
Wed Sep 30 21:32:12 CEST 2009


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)

-Ista

On Wed, Sep 30, 2009 at 8:31 AM, Dieter Menne
<dieter.menne at menne-biomed.de> wrote:
>
>
>
> David Winsemius wrote:
>>
>>
>>> # Typical C-Programmer style
>>> factor(levs[as.integer(data >10)+1], levels=levs)
>>
>> In your code the as.integer function is superfluous
>
> Oops... done too much c# lately, getting invalid cast challenged.
>
> Dieter
>
>
> --
> View this message in context: http://www.nabble.com/Condition-to-factor-%28easy-to-remember%29-tp25676411p25680111.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Ista Zahn
Graduate student
University of Rochester
http://yourpsyche.org




More information about the R-help mailing list