[R] Is there any R function for data normalization?

Duncan Murdoch murdoch.duncan at gmail.com
Tue Oct 2 11:59:39 CEST 2012


On 12-10-02 5:51 AM, Rui Esteves wrote:
> Hello,
>
> I have a matrix with values, with columns c1..cn.
> I need the values to be normalized between 0 and 1 by column.
> Therefor, the 0 should correspond to the minimum value in the column c1 and
> 1 should correspond to the maximum value in the column c1.
> The remaining columns should be organized in the same way.
>
> Does a function in R exists for this purpose?

No, but it is easy to construct one using sweep.  First subtract column 
mins, then divide by column maxes:

normalize <- function(x) {
   x <- sweep(x, 2, apply(x, 2, min))
   sweep(x, 2, apply(x, 2, max), "/")
}

Duncan Murdoch




More information about the R-help mailing list