Skip to content

command similar to colSums for rowSums?

7 messages · Will Carr, Peter Ehlers, David Winsemius

#
Working with an NxMxO sized matrix, currently I can do this in my code:

if (max(colSums(array)) >= number)

But to get an equivalent result using rowSums, I have to do:

for (i in 1:10)
{
if (max(rowSums(array[,,i])) >= number) 
}

I'm running both in a much larger loop that loops millions of times, so
speed and such is quite a big factor for me. Currently, the colSums line
uses about 1/10th as much time as the rowSums' for loop, and the for loop
actually took as much time as the rest of my code combined took to execute.
Is there a faster way than using a for loop and rowSums?
#
Does this

  max(apply(yourArray, 3, rowSums))

give you what you want?

  -Peter Ehlers
Will Carr wrote:
#
Or maybe aperm() is faster:

  max(colSums(aperm(yourArray, c(2,1,3))))

  -Peter Ehlers
Peter Ehlers wrote:
#
On Nov 30, 2009, at 11:10 AM, Will Carr wrote:

            
What are M, N, and O? All the same magnitude?
What are you doing based on that if( ) determination?
Are you sure you are interpreting your profile results correctly?
It may depend on what you are trying to do, which at the moment is  
most unclear.
David Winsemius, MD
Heritage Laboratories
West Hartford, CT
#
David Winsemius wrote:
In this case, 4,13,10.
Increasing a counter.
No, but I think I am. I'm using proc.time() at the beginning of my code and
end, differencing the elapsed time, and running it a few times to get an
average difference.
It may depend on what you are trying to do, which at the moment is  
most unclear.
David Winsemius, MD
Heritage Laboratories
West Hartford, CT

______________________________________________
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.
#
This gives the correct result but appears to be slower than the for loop I
was using, I do appreciate the suggestion though.
Peter Ehlers wrote:

  
    
#
This seems to work quite well and gives an ~85% speed increase compared to
the for loop. Thanks a bunch!
Peter Ehlers wrote: