[S] applying a function element-wise to two lists

Andreas Krause (Andreas.Krause@genedata.com)
Tue, 14 Apr 1998 16:39:12 +0200


I was wondering if there is an elegant solution to the following problem,
avoiding explicit looping.
Given two lists of same length, say x and y, whose elements are numeric
vectors. length(x[[i]] is equal to length(y[[i]]) for all i.
Calculate f (x[[i]], y[[i]]) for i in 1:length(x).

Example:

x <- list (1:5, 1:3, 1:8)
y <- list (1:5, 4:2, c(7:10, 14:11))
z <- list()
# the loop solution for f(a,b)=max(a/b)
for (i in 1:length(x)) z[[i]] <- max (x[[i]]/y[[i]])

(My real application is using a more complicate customized f.)

andreas krause

-- 
Dr. Andreas Krause, GeneData AG, Postfach 254, CH-4016 Basel
mailto:Andreas.Krause@genedata.com   http://www.genedata.com
-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu.  To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message:  unsubscribe s-news