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

Peter Dalgaard p.dalgaard at biostat.ku.dk
Wed Aug 25 22:51:34 CEST 2004


Paul Roebuck <roebuck at odin.mdacc.tmc.edu> writes:

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

Try looking at matplot() and friends...
 
> 
> 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)
> 
> ______________________________________________
> R-help at 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
> 

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list