Thanks, Peter and Henrik. Vasu.
On 11/30/05, Henrik Bengtsson <[EMAIL PROTECTED]> wrote: > > Vasundhara Akkineni wrote: > > I have another issue.i have a function which calculates the log2(col i > > /col2) value, where i stands for columns 3,4,...etc. > > > > data<-read.table("table.txt", header=TRUE) > > > > iratio<-function(x){ > > for(n in 3:ncol(data)){ > > z<-log2(data[x,n]/data[x,2]) > > } > > } > > > > Where x- the row number of the data frame(data). > > > > i want to store the ratios for each row in a object z, which can be > > accessed outside the function. How can i do this? > > 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. > > > Thanks, > > Vasu. > > > > [[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 > > > > > > [[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