Try
?mapply

Description:

     'mapply' is a multivariate version of 'sapply'.  'mapply' applies
     'FUN' to the first elements of each ...  argument, the second
     elements, the third elements, and so on.  Arguments are recycled
     if necessary.

Rich

On Wed, Apr 30, 2014 at 9:03 PM, Waichler, Scott R
<scott.waich...@pnnl.gov> wrote:
> Here is a working example with no random parts.  Thanks for your patience and 
> if I'm still off the mark with my presentation I'll drop the matter.
>
> v <- c(NA, 1.5, NA, NA,
>        NA, 1.1, 0.5, NA,
>        NA, 1.3, 0.4, 0.9)
> a1 <- array(v, dim=c(2,2,3))
> m1 <- matrix(c(NA, 1.5, 2.1, NA), ncol=2, byrow=T)
> m2 <- matrix(c(1.56, 1.64, 1.16, 2.92), ncol=2)
> condition1 <- !is.na(m1)& m1 > m2
>
> ans <- matrix(NA, ncol=2, nrow=2) # initialize
> for(i in 1:2) {
>   for(j in 1:2) {
>     ind.not.na <- which(!is.na(a1[i,j,]))
>     if(condition1[i,j] && length(ind.not.na) > 0) ans[i,j] <- 
> a1[i,j,ind.not.na[1]] + m2[i,j]
>   }
> }
> ans
>      [,1] [,2]
> [1,]   NA 1.66
> [2,] 3.14   NA
>
> Let me try asking again in words.  If I have multiple matrices or slices of 
> 3d arrays that are the same dimension, is there a way to pass them all to 
> apply, and have apply take care of looping through i,j?  I understand that 
> apply has just one input object x.  I want to work on more than one array 
> object at once using a custom function that has this characteristic:  in 
> order to compute the answer at i,j I need a result from higher order array at 
> the same i,j.  This is what I tried to demonstrate in my example above.
>
> Thanks,
> Scott
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to