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

Andreas Krause (Andreas.Krause@genedata.com)
Tue, 14 Apr 1998 17:59:26 +0200


As the responses came in so quickly, here is a first summary.

x <- list (1:5, 1:3, 1:8)
y <- list (1:5, 4:2, c(7:10, 14:11))
x <- rep (x, 15000)
y <- rep (y, 15000)
z <- list (); for (i in 1:length(x)) z[[i]] <- max (x[[i]]/y[[i]])

takes about 488 seconds on an SGI running Splus 3.4.

Bill Dunlap wrote:
> z <- lapply(seq(along=x), function(i,x,y)max(x[[i]]/y[[i]]), x, y)

takes about 62 seconds. Christian Keller proposed the same approach.
It makes quite a difference the bigger the data gets.

Jerome Asselin's variant
z <- apply(matrix(1:length(x)), 1, function(i, x, y) max(x[[i]]/y[[i]])
, x = x, y = y)
needed about 80 seconds, using a very similar approach.

Thanks to all of you!

andreas

-- 
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