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? Thanks, Rui [[alternative HTML version deleted]]
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
In this case you could use the apply function. Let your k*l matrix is named as y. Then, in order to standardize the values within each column use the following function aver<-apply(y,2,mean) # calculate the mean within each column std<-apply(y,2,sd) # calculate the stabdard deviation within each column z<-matrix(0,nrow=k,ncol=l) for(i in 1:k){ for(j in 1:l){ z[i,j]<-(y[i,j]-aver[j])/std[j] } } z On Tue, Oct 2, 2012 at 12:51 PM, Rui Esteves <ruimaximo@gmail.com> 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? > > Thanks, > Rui > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
Hi Rui, It doesn't really need one... doit <- function(x) {(x - min(x, na.rm=TRUE))/(max(x,na.rm=TRUE) - min(x, na.rm=TRUE))} # use lapply to apply doit() to every column in a data frame # mtcars is built into R normed <- as.data.frame(lapply(mtcars, doit)) # very that the range of all is [0, 1] lapply(normed, range) Cheers, Josh On Tue, Oct 2, 2012 at 2:51 AM, Rui Esteves <ruimaximo at gmail.com> 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? > > Thanks, > Rui > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.-- Joshua Wiley Ph.D. Student, Health Psychology Programmer Analyst II, Statistical Consulting Group University of California, Los Angeles https://joshuawiley.com/
Hello, ?scale in "base" package. Best Regards, Pascal Le 12/10/02 18:51, Rui Esteves a ?crit :> 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? > > Thanks, > Rui > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >
Hello, Try the following. fun <- function(x){ a <- min(x) b <- max(x) (x - a)/(b - a) } mat <- matrix(rnorm(12), ncol=3) apply(mat, 2, fun) Hope this helps, Rui Barradas Em 02-10-2012 10:51, Rui Esteves escreveu:> 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? > > Thanks, > Rui > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.