[R] Scaling values 0-255 -> -1 , 1 - how can this be done?

David Winsemius dwinsemius at comcast.net
Wed Nov 21 20:12:21 CET 2012


On Nov 21, 2012, at 7:32 AM, Brian Feeny wrote:

>
> I have a dataframe in which I have values 0-255, I wish to transpose  
> them such that:
>
> if value  > 127.5 value = 1
> if value < 127.5 value = -1
>

c(-1, 1)[ 1+(value > 127.5) ]

I suspect most will find this less intuitive than `ifelse`, but I find  
it useful when picking colors or other values from a vector,  
expecially when the argument is built with findInterval, e.g.

c(-1, 1)[ findInterval(value, c(0, 127.5, 255) ) ] Which generalizes  
much more compactly to multiple intervals than does ifelse.


> I did something similar using the "binarize" function of the biclust  
> package, this transforms my dataframe to 0 and 1 values, but I wish
> to use -1 and 1 and looking for a way in R to do this.
-- 

David Winsemius, MD
Alameda, CA, USA




More information about the R-help mailing list