[R] mode across lists of matrices

2010-09-21 Thread Gregory Ryslik
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

Re: [R] mode across lists of matrices

2010-09-21 Thread Henrique Dallazuanna
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

Re: [R] mode across lists of matrices

2010-09-21 Thread Gregory Ryslik
Hi, That doesn't work as seen in this example. I get a 4x3 matrix as the answer which is definitely not right! But I think we're getting closer! Cheers, G 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

Re: [R] mode across lists of matrices

2010-09-21 Thread Gregory Ryslik
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

Re: [R] mode across lists of matrices

2010-09-21 Thread Henrique Dallazuanna
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

Re: [R] mode across lists of matrices

2010-09-21 Thread Gregory Ryslik
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),

Re: [R] mode across lists of matrices

2010-09-21 Thread Henrique Dallazuanna
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

Re: [R] mode across lists of matrices

2010-09-21 Thread Gregory Ryslik
Perfect! Thank you! 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

Re: [R] mode across lists of matrices

2010-09-21 Thread Gregory Ryslik
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