[R] Scaling data

Peter Dalgaard P.Dalgaard at biostat.ku.dk
Mon Sep 28 16:13:56 CEST 2009


Jim Lemon wrote:
> On 09/28/2009 07:55 PM, Dry, Jonathan R wrote:
>> Hello all
>>
>> I have a data frame representing a matrix of data.  For each of my
>> variables (rows) I want to scale the data between 0 (representing the
>> minimum value in that row) and 1 (representing the maximum value in
>> that row).  I was wondering if there is a simple function anywhere
>> that does this?
>>
>>    
> Hi Jonathan,
> There are a number of such functions, but a particularly easy one in
> your case is rescale. Baptiste has already mentioned the one in ggplot,
> so I'll plug mine in plotrix:
> 
> mat1<-matrix(sample(1:20,16),nrow=4)
> library(plotrix)
> t(apply(mat1,1,rescale,c(0,1)))
> 

Notice also that scale() will do it if you tickle it in the right places:

> x  <- rnorm(500)
> range(scale(x, min(x), diff(range(x))))
[1] 0 1


-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)              FAX: (+45) 35327907




More information about the R-help mailing list