Try this:

mode <- function(x, ...)
    as.numeric(names(which.max(table(x))))
apply(array(unlist(mymats), dim = c(length(mymats), dim(mymats[[1]]))), 1:2,
mode)


On Tue, Sep 21, 2010 at 10:47 AM, Gregory Ryslik <rsa...@comcast.net> wrote:

> Hi Everyone,
>
> I am interested in taking the mode over several thousand matrices. I show
> an example below. For the [1,1] entry of my "mode" matrix that I want to
> create I would like to have a "2". For the [1,2] entry I would want a 2.
>  For the [2,2] entry it would be 4 and so forth. Earlier, I was working with
> continuous cases and thus each (n,m) element was simply an average. I was
> able to then do element-wise addition and counting using the "Reduce"
> function and then the average would be totalsum/totalcount where the NA
> terms were discounted. Here, it's not exactly a binary case so Reduce
> doesn't quite work as well. I'm open to suggestions but would as always like
> to avoid long loops as that will significantly bump up running time over
> several thousand trees. Similarly, I would not like to do a lot of sorts to
> find the mode either...
>
> Thanks for your help!
>
> mymats
> [[1]]
>     [,1] [,2] [,3]
> [1,]    0    2    1
> [2,]    2    3    3
> [3,]    2    1    2
>
> [[2]]
>     [,1] [,2] [,3]
> [1,]    1    2    4
> [2,]    2    4    4
> [3,]    3    4    5
>
> [[3]]
>     [,1] [,2] [,3]
> [1,]    2    3    1
> [2,]    3    4    2
> [3,]    5    1    3
>
> [[4]]
>     [,1] [,2] [,3]
> [1,]    2    4    2
> [2,]    1   NA    2
> [3,]    2    3    1
>
> [[5]]
>     [,1] [,2] [,3]
> [1,]   NA    2    1
> [2,]    2    4    1
> [3,]    1    3    2
> ______________________________________________
> 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.
>



-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

        [[alternative HTML version deleted]]

______________________________________________
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