Søren Højsgaard
2008-Mar-05 00:13 UTC
[R] How to make a 2-dim array being "interpreted" as an array rather than as a matrix
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" 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??? Thanks in advance S?ren
Henrique Dallazuanna
2008-Mar-05 00:22 UTC
[R] How to make a 2-dim array being "interpreted" as an array rather than as a matrix
I don't know if is the best way: a3 <- array(a2, dim=c(nrow(a2), ncol(a2), 1)) foo(a3) On 04/03/2008, S?ren H?jsgaard <Soren.Hojsgaard at agrsci.dk> 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" > > 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??? > > Thanks in advance > S?ren > > > > > > > > ______________________________________________ > R-help at 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
Erik Iverson
2008-Mar-05 00:30 UTC
[R] How to make a 2-dim array being "interpreted" as an array rather than as a matrix
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
Erik Iverson
2008-Mar-05 00:41 UTC
[R] How to make a 2-dim array being "interpreted" as an array rather than as a matrix
One more note:>> 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??? >Maybe try? a3 <- structure(1:10, dim = c(2,5), class = "array") Then class(a3) gives "array", and is.matrix and is.array are both still TRUE. Erik