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.