[R] Difference across the Nth dimension of an array

Vincent Zoonekynd zoonek at gmail.com
Mon Jan 9 04:10:40 CET 2012

On 9 January 2012 08:53, Phil Wiles <philip.wiles at gmail.com> wrote:
> I have a multidimensional array - in this case with 4 dimensions of x,y,z
> and time.  I'd like to take the time derivative of this matrix, i.e.
> perform the diff operator along dimension number 4.

"apply" can do that, but you may need to reorder the dimensions of the result.

  a <- array(1:81,dim=c(3,3,3,3))
  b <- apply(a, 1:3, diff)
  b <- aperm(b,c(2:4,1))

-- Vincent

More information about the R-help mailing list