[R] Vectorise a for loop?

Jacques VESLOT jacques.veslot at good.ibl.fr
Tue Sep 26 15:01:37 CEST 2006


tt$fold <- ifelse(tt$M < 0, 1/(2^tt$M), 2^tt$M)
-------------------------------------------------------------------
Jacques VESLOT

CNRS UMR 8090
I.B.L (2ème étage)
1 rue du Professeur Calmette
B.P. 245
59019 Lille Cedex

Tel : 33 (0)3.20.87.10.44
Fax : 33 (0)3.20.87.10.31

http://www-good.ibl.fr
-------------------------------------------------------------------


john seers (IFR) a écrit :
>  
> Hi R guru coders
>  
> I wrote a bit of code to add a new column onto a "topTable" dataframe.
> That is a list of genes processed using the limma package. I used a for
> loop but I kept feeling there was a better way using a more vector
> oriented approach. I looked at several commands such as "apply", "by"
> etc but could not find a good way to do it. I have this feeling there is
> a command or technique eluding me. (Is there an expr:value1?value2
> construction in R?) 
>  
> Can anybody suggest an elegant solution? 
>  
> Details:
>  
> So, the topTable looks like this:
>  
> 
>>topa1[1:5,c(1,2,3,4)]
> 
>           ID    Name GB_accession         M
> 11195 245828 SIGKEC9     AX135029 -7.670197
> 10966    107    FHL1       B14446 -5.089926
> 6287   25744     M90    LL137340 -4.531744
> 777     2288   VSNL1     LF039555 -4.035472
> 11310 272294     M98    LL031650  3.866422
> 
> 
> I want to add a "fold" column so it will look like this:
>  
> 
>>topa1[1:5,c(1,2,3,4,10)]
> 
>           ID    Name GB_accession         M      fold
> 11195 245828 SIGKEC9     AX135029 -7.670197 203.68521
> 10966    107    FHL1       B14446 -5.089926  34.05810
> 6287   25744     M90    LL137340 -4.531744  23.13082
> 777     2288   VSNL1     LF039555 -4.035472  16.39828
> 11310 272294     M98    LL031650  3.866422  14.58508
> 
> 
>  
> The fold values is calculated from the M column which is a log2 value.
> The calculation is different depending on whether the M value is
> negative or positive. That is if the gene is down regulated the
> reciprocal value has to be used to calculate a fold value.
>  
> Here is my clunky, not vectorised code :
>  
> # Function to add a fold column to the toptable
> ttfold<-function(tt) {
>  fold<-NULL
>  for (i in 1:length(tt$M)) {
>   if (tt$M[i] < 0 ) {
>    fold[i]<-1/(2^tt$M[i])
>   } else {
>    fold[i]<-2^tt$M[i]
>   }
>  }
>  tt<-cbind(tt, fold) 
> }
> 
> # Add fold column to top tables
> topa1<-ttfold(topa1)
>  
>  
>  
>  
> Regards
>  
>  
> J
>  
>  
>  
>  
>  
>  
>  
>  
>  
> ---
> 
> John Seers
> Institute of Food Research
> Norwich Research Park
> Colney
> Norwich
> NR4 7UA
>  
> 
> tel +44 (0)1603 251497
> fax +44 (0)1603 507723
> e-mail john.seers at bbsrc.ac.uk <mailto:john.seers at bbsrc.ac.uk>
> 
> e-disclaimer at http://www.ifr.ac.uk/edisclaimer/
> <http://www.ifr.ac.uk/edisclaimer/>  
>  
> Web sites:
> 
> www.ifr.ac.uk <http://www.ifr.ac.uk/>    
> www.foodandhealthnetwork.com <http://www.foodandhealthnetwork.com/> 
>  
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> 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.
>



More information about the R-help mailing list