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

Jerome Asselin (jasselin@mat.ulaval.ca)
Tue, 14 Apr 1998 10:51:21 -0400 (EDT)


I suggest the following command to avoid explicit looping

x <- list (1:5, 1:3, 1:8)
y <- list (1:5, 4:2, c(7:10, 14:11))

z <- apply(matrix(1:length(x)), 1, function(i, x, y)
max(x[[i]]/y[[i]])
, x = x, y = y)

# If necessary, you can add
mode(z) <- "list"

# giving the following result
> z
[[1]]:
[1] 1

[[2]]:
[1] 1.5

[[3]]:
[1] 0.7272727

Jérôme Asselin
Student

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