Skip to content

Row wise function call.

1 message · Henrik Bengtsson

#
Vasundhara Akkineni wrote:
Just return the result at the end of the function.  Since you want to 
return log-ratios for many pairs, I would suggest to stick them in a new 
data frame, or since they results are all of the same data type, a 
matrix instead. Example:

iratio <- function(rows) {
   # Create (pre-allocate) empty matrix poulated with NAs
   z <- matrix(NA, nrow=length(x), ncol=ncol(data)-3+1);

   for(col in 3:ncol(data)) {
     z[,col-3+1] <- log2(data[rows,col] / data[rows,2])
   }

   z;  # or equivalent 'return(z)'; not often seen in R code.
}

You should try to look into introductions to R (sorry I don't know any 
off hand, but just search google); this is all things you need to learn. 
Also, you learn a lot from reading other people's code snippets.

When you understand R better, you'll also realize that the above can be 
done in one line of code like this:

   z <- log2(data[rows,3:ncol(data)] / data[rows,2]);

Cheers

Henrik

PS. I haven't actually tested the above code in R; there might be typos. DS.