Hi, I think there's some rounding issue with returning the max column. (running 2.9.0 on an Apple, but my buddy found it on his PC) > x <- matrix(c(1234.568,1234.569,1234.567),1) > max.col(x) [1] 2 > x <- matrix(c(12345.568,12345.569,12345.567),1) > max.col(x) [1] 3 > x <- matrix(c(112345.568,112345.569,112345.567),1) > max.col(x) [1] 3 > max.col(-x) [1] 1 Thanks, Daryl
max.col weirdness
4 messages · Daryl Morris, Bert Gunter, Ben Bolker
Hi, Sorry, I was too hasty. I see that the tolerance is in the documentation (and that "random" is the default tie-breaker). Would it be possible to allow tolerance to be a parameter? thanks, Daryl
Daryl Morris wrote:
Hi, I think there's some rounding issue with returning the max column. (running 2.9.0 on an Apple, but my buddy found it on his PC)
x <- matrix(c(1234.568,1234.569,1234.567),1) max.col(x)
[1] 2
x <- matrix(c(12345.568,12345.569,12345.567),1) max.col(x)
[1] 3
x <- matrix(c(112345.568,112345.569,112345.567),1) max.col(x)
[1] 3
max.col(-x)
[1] 1 Thanks, Daryl
Try reading the man page, which says: Details When ties.method = "random", as per default, ties are broken at random. In this case, the determination of a tie assumes that the entries are probabilities: there is a relative tolerance of 1e-5, relative to the largest (in magnitude, omitting infinity) entry in the row. Bert Gunter Genentech Nonclinical Biostatistics -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Daryl Morris Sent: Thursday, May 28, 2009 1:47 PM To: r-help at r-project.org Subject: [R] max.col weirdness Hi, I think there's some rounding issue with returning the max column. (running 2.9.0 on an Apple, but my buddy found it on his PC) > x <- matrix(c(1234.568,1234.569,1234.567),1) > max.col(x) [1] 2 > x <- matrix(c(12345.568,12345.569,12345.567),1) > max.col(x) [1] 3 > x <- matrix(c(112345.568,112345.569,112345.567),1) > max.col(x) [1] 3 > max.col(-x) [1] 1 Thanks, Daryl ______________________________________________ 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.
Daryl Morris wrote:
Hi, Sorry, I was too hasty. I see that the tolerance is in the documentation (and that "random" is the default tie-breaker). Would it be possible to allow tolerance to be a parameter? thanks, Daryl
Could you just use apply(x,1,which.max) ?
x <- matrix(c(112345.568,112345.569,112345.567),1) x
[,1] [,2] [,3] [1,] 112345.6 112345.6 112345.6
apply(x,1,which.max)
[1] 2 It will presumably be less efficient than max.col (which is implemented in C), and will automatically pick the first value of the maximum if there are ties, but should be OK for everyday use? PS oddly enough apply(x,1,which.max) is faster -- maybe because it doesn't try to deal with ties?
benchmark(max.col(x),apply(x,1,which.max),replications=50000)
test replications user.self sys.self elapsed user.child 2 apply(x, 1, which.max) 50000 7.649 0.456 8.110 0 1 max.col(x) 50000 11.185 0.580 11.828 0 sys.child 2 0 1 0
View this message in context: http://www.nabble.com/max.col-weirdness-tp23769831p23770927.html Sent from the R help mailing list archive at Nabble.com.