[R] [Q] Apply Function Over Multiple Array Margins

Paul Roebuck roebuck at odin.mdacc.tmc.edu
Wed Aug 25 22:42:36 CEST 2004


Is there a canonical means to apply a function
over multiple arrays simultaneously? For example,
if I wanted to plot each line with a different color?
Or is a for loop conversion my best option?


x <- seq(0, 8, by = 2)
y <- matrix(1:15, nrow = 5, byrow = TRUE)
my.colors <- heat.colors(3)

drawLines <- function(row) {
    lines(x, row)    # want corresponding 'my.colors' here
}

plotData <- function(x, y) {
    plot(x, type='n', axes = FALSE,
         xlim = c(min(x), max(x)),
         ylim = c(0, max(y)));
    box();
    axis(1, min(x):max(x))
    axis(2, seq(0, max(y), by = 5))
    axis(3, labels = FALSE)
    axis(4, labels = FALSE)
    apply(y, 1, drawLines)
}

plotData(x, t(y))

----------------------------------------------------------
SIGSIG -- signature too long (core dumped)




More information about the R-help mailing list