Skip to content
Back to formatted view

Raw Message

Message-ID: <506ABB0B.6010004@gmail.com>
Date: 2012-10-02T09:59:39Z
From: Duncan Murdoch
Subject: Is there any R function for data normalization?
In-Reply-To: <CAJYLPFejf20szBgBHeSvo6LpyNgs5d+Y_+HUJEN1c-6xV2Ugsw@mail.gmail.com>

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