[R] question about apply function

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.be
Fri Dec 8 09:48:41 CET 2006


in this case you may use something like the following:

DATA <- matrix(1:5, 5, 3)
dimnames(DATA) <- list(c("S1","S2","S3","S4","S5"), c("V1","V2","V3"))
#####################
DATA / rowSums(DATA)
DATA / rep(colSums(DATA), each = nrow(DATA))


I hope it helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
     http://www.student.kuleuven.be/~m0390867/dimitris.htm


----- Original Message ----- 
From: "Andris Jankevics" <andza at osi.lv>
To: <r-help at stat.math.ethz.ch>
Sent: Friday, December 08, 2006 9:36 AM
Subject: [R] question about apply function


> Dear R-Users,
>
> For example i have a data matrix with five samples and three 
> variables.
>
> DATA <- 
> matrix(c(1,1,1,2,2,2,3,3,3,4,4,4,5,5,5),nrow=5,ncol=3,byrow=TRUE)
> colnames (DATA) <- c("V1","V2","V3")
> rownames (DATA) <- c("S1","S2","S3","S4","S5")
>
> I want to normalize all samples to same sum of variables:
>
> NormFun <- function (i) {(i*(1/sum(i)))}
>
> Dnorm <- apply(DATA,1,NormFun)
>
> Why I am getting tranposed matrix Dnorm? And with my experimental 
> data (with
> 32k variables) i am getting a slighty different results from:
>
> apply(DATA,1,NormFun)
> apply(t(DATA),2,NormFun)
>
> Thankyou,
>
> Andris Jankevics
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
> 


Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm




More information about the R-help mailing list