Skip to content
Back to formatted view

Raw Message

Message-ID: <47CDE9A9.2040700@biostat.wisc.edu>
Date: 2008-03-05T00:30:33Z
From: Erik Iverson
Subject: How to make a 2-dim array being "interpreted" as an array rather than as a matrix
In-Reply-To: <C83C5E3DEEE97E498B74729A33F6EAEC0562FBE0@DJFPOST01.djf.agrsci.dk>

Hello -

S?ren H?jsgaard wrote:
> Dear List
> 
> A 2-dimensional array seems to be "interpreted" as a matrix whereas
> higher dimensional arrays are interpreted as arrays, e.g.
> 
>> a1 <- array(1:8,c(2,2,2)) class(a1)
> [1] "array"
>> a2 <- array(1:4,c(2,2)) class(a2)
> [1] "matrix"
> 

Note that is.matrix(a2) and is.array(a2) are both TRUE though.

> If I write a generic function (apologies if this is the wrong word)
> on arrays as
> 
> foo <- function(x) UseMethod("foo") foo.array <- function(x) sum(x)
> 
> then this fails on a 2-dimensional array:
> 
>> foo(a1)
> [1] 36
>> foo(a2)
> Error in UseMethod("foo") : no applicable method for "foo"
> 
> Is there any way in which I can "force" a2 to being "interpreted" as
> having class "array" rather than "matrix" so that foo will work on
> a2???

I don't know about that.  However, I see that both of the functions
duplicated.matrix and unique.matrix are copies of duplicated.array and 
unique.array, respectively.

Perhaps that's all you need to do?

foo.matrix <- foo.array

Maybe there is a better suggestion?

Best,
Erik Iverson