Brian Feeny
2012-Nov-21 15:32 UTC
[R] Scaling values 0-255 -> -1 , 1 - how can this be done?
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 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. Brian
Marc Schwartz
2012-Nov-21 17:11 UTC
[R] Scaling values 0-255 -> -1 , 1 - how can this be done?
On Nov 21, 2012, at 9:32 AM, Brian Feeny <bfeeny at me.com> 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 > > 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. > > BrianSee ?ifelse ifelse(value > 127.5, 1, -1) You might want to think about what happens when value == 127.5 ... Regards, Marc Schwartz
Sarah Goslee
2012-Nov-21 17:12 UTC
[R] Scaling values 0-255 -> -1 , 1 - how can this be done?
fakedata <- data.frame(matrix(sample(1:255, 50, replace=TRUE), ncol=5)) ifelse(fakedata > 127.5, 1, -1) Sarah On Wed, Nov 21, 2012 at 10:32 AM, Brian Feeny <bfeeny at me.com> 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 > > 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. > > Brian >-- Sarah Goslee http://www.functionaldiversity.org
Rainer Schuermann
2012-Nov-21 17:20 UTC
[R] Scaling values 0-255 -> -1 , 1 - how can this be done?
x <- as.data.frame( matrix( 0:255, nrow = 16 ) ) ifelse( x > 127.5, 1, -1 ) Is that what you want? Rgds, Rainer On Wednesday 21 November 2012 10:32:49 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 > > 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. > > Brian > > ______________________________________________ > 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.
HI, You could also use: set.seed(5) ?x1<-data.frame(matrix(sample(0:255,80,replace=TRUE),ncol=10)) library(car) ?do.call(cbind,lapply(x1,function(x) x<-recode(x,"0:127.5=-1;127.6:255=1"))) #???? X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 #[1,] -1? 1 -1 -1 -1? 1? 1 -1? 1? -1 #[2,]? 1 -1? 1 -1 -1 -1? 1 -1? 1?? 1 #[3,]? 1 -1? 1 -1 -1? 1 -1 -1? 1? -1 #[4,] -1 -1? 1? 1 -1? 1? 1 -1 -1?? 1 #[5,] -1 -1? 1 -1? 1? 1? 1? 1? 1? -1 #[6,]? 1? 1? 1? 1? 1? 1? 1 -1? 1?? 1 #[7,]? 1 -1 -1 -1 -1? 1? 1? 1? 1? -1 #[8,]? 1 -1 -1 -1 -1 -1 -1? 1? 1? -1 A.K. ----- Original Message ----- From: Brian Feeny <bfeeny at me.com> To: r-help at r-project.org Cc: Sent: Wednesday, November 21, 2012 10:32 AM Subject: [R] Scaling values 0-255 -> -1 , 1 - how can this be done? 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 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. Brian ______________________________________________ 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.
S Ellison
2012-Nov-21 18:29 UTC
[R] Scaling values 0-255 -> -1 , 1 - how can this be done?
> Subject: Re: [R] Scaling values 0-255 -> -1 , 1 - how can > this be done?#Also by taking advantage of numerical interpretation of booleans: x<- sample(255) #toy data 2*( x > 127.5 ) - 1 #S Ellison ******************************************************************* This email and any attachments are confidential. Any use...{{dropped:8}}
David Winsemius
2012-Nov-21 19:12 UTC
[R] Scaling values 0-255 -> -1 , 1 - how can this be done?
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