[R] Vectorise a for loop?

From: john seers (IFR) <john.seers_at_bbsrc.ac.uk>
Date: Tue 26 Sep 2006 - 12:52:21 GMT

 

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@bbsrc.ac.uk <mailto:john.seers@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@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.
Received on Tue Sep 26 22:59:29 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 26 Sep 2006 - 13:30:07 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.