I tried the following:

m <- matrix(runif(100000),1000,100)
junk <- gc()
print(system.time(for(i in 1:100) X1 <- do.call(pmax,data.frame(m))))
junk <- gc()
print(system.time(for(i in 1:100) X2 <- apply(m,1,max)))

and got

   user  system elapsed
  2.704   0.110   2.819
   user  system elapsed
  1.938   0.098   2.040

so unless there's something that I am misunderstanding (always a serious
consideration) Wacek's apply method looks to be about 1.4 times *faster* than
the do.call/pmax method.

        cheers,

                Rolf Turner


On 30/03/2009, at 3:55 PM, Bert Gunter wrote:

If speed is a consideration,availing yourself of the built-in pmax()
function via

do.call(pmax,data.frame(yourMatrix))

will be considerably faster for large matrices.

If you are puzzled by why this works, it is a useful exercise in R to figure
it out.

Hint:The man page for ?data.frame says:
"A data frame is a list of variables of the same length with unique row
names, given class 'data.frame'."

Cheers,
Bert

Bert Gunter
Genentech Nonclinical Statistics

-----Original Message-----
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r- project.org] On
Behalf Of Wacek Kusnierczyk
Sent: Saturday, March 28, 2009 5:22 PM
To: Ana M Aparicio Carrasco
Cc: r-help@r-project.org
Subject: Re: [R] Matrix max by row

Ana M Aparicio Carrasco wrote:
I need help about how to obtain the max by row in a matrix.
For example if I have the following matrix:
2 5 3
8 7 2
1 8 4

The max by row will be:
5
8
8


matrix(apply(m, 1, max), nrow(m))

vQ

______________________________________________
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.

______________________________________________
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.


######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}

______________________________________________
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