Hi Everyone,

Looks like I found the solution, the mode function is now changed to: 
mode <- function(x){
        cell.mode<-as.numeric(names(which.max(table(x))))
        if(length(cell.mode)==0){
                cell.mode <- NA
                }
        cell.mode
        }       

Then you can take out the useNA = 'ifAny' in the apply line. Thanks everyone 
and especially Henrique for all your help!

Kind regards,
Greg


Begin forwarded message:

> From: Gregory Ryslik <rsa...@comcast.net>
> Date: September 21, 2010 4:08:33 PM EDT
> To: Henrique Dallazuanna <www...@gmail.com>
> Cc: r-h...@stat.math.ethz.ch
> Subject: Re: [R] mode across lists of matrices
> 
> Hey,
> 
> Looks like I spoke to soon last time. It's almost there but not quite. Now If 
> there are more NA's than any other number, it returns NA. That's not the 
> correct behavior though as I want to return NA only if all the numbers are 
> NA. For example, in the situation below, I want it to return 5 for element 
> [1,1].  Thanks though for all your help!  
> 
> If I take out the "useNA = ifany" then I get a "numeric, 0" if all were NA 
> and the correct number if at least some were not NA. Is there a way I can 
> test for the "numeric, 0" at the end when the matrix is constructed and swap 
> them out? Or is there a simpler way?
> 
> Thanks again,
> Greg
> 
> mymats <- vector('list', 4)
> set.seed(246)
> 
> # Generate a list of five 3 x 3 matrices
> for(i in 1:4) mymats[[i]] <- matrix(sample(1:9), nrow = 3)
> 
> mymats[[1]][1,1]<-NA
> mymats[[1]][1,2]<-3
> mymats[[1]][1,3]<-1
> mymats[[2]][2,1]<-2
> mymats[[1]][2,2]<-3
> mymats[[1]][2,3]<-1
> mymats[[1]][3,1]<-2
> mymats[[1]][3,2]<-2
> mymats[[1]][3,3]<-3
> 
> mymats[[2]][1,1]<-NA
> mymats[[2]][1,2]<-3
> mymats[[2]][1,3]<-2
> mymats[[2]][2,1]<-1
> mymats[[2]][2,2]<-2
> mymats[[2]][2,3]<-2
> mymats[[2]][3,1]<-1
> mymats[[2]][3,2]<-3
> mymats[[2]][3,3]<-2
> 
> mymats[[3]][1,1]<-NA
> mymats[[3]][1,2]<-2
> mymats[[3]][1,3]<-2
> mymats[[3]][2,1]<-2
> mymats[[3]][2,2]<-3
> mymats[[3]][2,3]<-1
> mymats[[3]][3,1]<-2
> mymats[[3]][3,2]<-2
> mymats[[3]][3,3]<-3
> 
> mymats[[4]][1,1]<-5
> mymats[[4]][1,2]<-1
> mymats[[4]][1,3]<-NA
> mymats[[4]][2,1]<-3
> mymats[[4]][2,2]<-2
> mymats[[4]][2,3]<-3
> mymats[[4]][3,1]<-1
> mymats[[4]][3,2]<-2
> mymats[[4]][3,3]<-1
> 
> mymats
> 
> 
> mode <- function(x){
>       as.numeric(names(which.max(table(x,useNA = 'ifany'))))
>       }
> answer <- apply(array(unlist(mymats), dim = c(dim(mymats[[1]]), 
> length(mymats))), 1:2, mode)
> 
> 
> 
> On Sep 21, 2010, at 3:27 PM, Henrique Dallazuanna wrote:
> 
>> Use this function:
>> 
>> mode <- function(x, ...)
>>     as.numeric(names(which.max(table(x, useNA = 'ifany')))) 
>> 
>> On Tue, Sep 21, 2010 at 4:18 PM, Gregory Ryslik <rsa...@comcast.net> wrote:
>> Hi,
>> 
>> Much better! That seems to work great. The only time it doesn't work is that 
>> if all the elements are NA for a specific element. Then it gives me a 
>> numeric(0) for that cell rather than NA.
>> 
>> On Sep 21, 2010, at 2:30 PM, Henrique Dallazuanna wrote:
>> 
>>> Try this:
>>> 
>>>  apply(array(unlist(mymats), dim = c(dim(mymats[[1]]), length(mymats))), 
>>> 1:2, mode)
>>> 
>>> The error was in c(length(mymats), dim(mymats[[1]]))
>>> 
>>> On Tue, Sep 21, 2010 at 3:19 PM, Gregory Ryslik <rsa...@comcast.net> wrote:
>>> Ack,  apologies for the previous email. What I meant to say is that the 
>>> first element is calculated incorrectly (1,1) should be 2, instead it is 3. 
>>> I've been staring at the code for two long. I've copied it in again for 
>>> convenience.
>>> 
>>> mymats <- vector('list', 4)
>>> set.seed(246)
>>> 
>>> # Generate a list of 4 3 x 3 matrices
>>> for(i in 1:4) mymats[[i]] <- matrix(sample(1:9), nrow = 3)
>>> 
>>> mymats[[1]][1,1]<-3
>>> mymats[[1]][1,2]<-3
>>> mymats[[1]][1,3]<-1
>>> mymats[[2]][2,1]<-2
>>> mymats[[1]][2,2]<-3
>>> mymats[[1]][2,3]<-1
>>> mymats[[1]][3,1]<-2
>>> mymats[[1]][3,2]<-2
>>> mymats[[1]][3,3]<-3
>>> 
>>> mymats[[2]][1,1]<-2
>>> mymats[[2]][1,2]<-3
>>> mymats[[2]][1,3]<-2
>>> mymats[[2]][2,1]<-1
>>> mymats[[2]][2,2]<-2
>>> mymats[[2]][2,3]<-2
>>> mymats[[2]][3,1]<-1
>>> mymats[[2]][3,2]<-3
>>> mymats[[2]][3,3]<-2
>>> 
>>> mymats[[3]][1,1]<-NA
>>> mymats[[3]][1,2]<-2
>>> mymats[[3]][1,3]<-2
>>> mymats[[3]][2,1]<-2
>>> mymats[[3]][2,2]<-3
>>> mymats[[3]][2,3]<-1
>>> mymats[[3]][3,1]<-2
>>> mymats[[3]][3,2]<-2
>>> mymats[[3]][3,3]<-3
>>> 
>>> mymats[[4]][1,1]<-2
>>> mymats[[4]][1,2]<-1
>>> mymats[[4]][1,3]<-2
>>> mymats[[4]][2,1]<-3
>>> mymats[[4]][2,2]<-2
>>> mymats[[4]][2,3]<-3
>>> mymats[[4]][3,1]<-1
>>> mymats[[4]][3,2]<-2
>>> mymats[[4]][3,3]<-1
>>> 
>>> mymats
>>> 
>>> 
>>> 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 Sep 21, 2010, at 10:08 AM, Henrique Dallazuanna wrote:
>>> 
>>>> 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
>>> 
>>> 
>>> 
>>> 
>>> -- 
>>> Henrique Dallazuanna
>>> Curitiba-Paraná-Brasil
>>> 25° 25' 40" S 49° 16' 22" O
>> 
>> 
>> 
>> 
>> -- 
>> 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