[R] command similar to colSums for rowSums?

Peter Ehlers ehlers at ucalgary.ca
Mon Nov 30 18:14:10 CET 2009


Does this

  max(apply(yourArray, 3, rowSums))

give you what you want?

  -Peter Ehlers

Will Carr wrote:
> 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?




More information about the R-help mailing list