[R] matching on multiple minimums

Adaikalavan Ramasamy ramasamy at cancer.org.uk
Thu Oct 28 17:04:44 CEST 2004


I cannot make sense of your question. There is no reproducible example
to help either. Please read the posting guide.

However, I think you might find using arr.ind=TRUE option in which()
when dealing with matrices to be useful. See help(which). Example :


mat <- matrix( 1:9, nc=3 ) + 100
mat[3,1] <- 101
mat
     [,1] [,2] [,3]
[1,]  101  104  107
[2,]  102  105  108
[3,]  101  106  109


which( mat == min(mat), arr.ind=T )
     row col
[1,]   1   1
[2,]   3   1


m1 <- mat[ ,1]
which( m1 == min(m1) )
[1] 1 3



On Thu, 2004-10-28 at 15:14, Barry Taylor wrote:
> Hi,
> 
> I was wondering if someone has witnessed the following
> behavior with matching on multiple minimums in a
> vector:
> 
> If I manually create a vector of doubles:
> test <- c(2.33,11.09,0.02,0.02.1.7,6.41)
> 
> I can then run any of the following to retrieve the
> indices of the minimum values (3 and 4 in this case),
> of which there are two (done purposefully):
> 
> which(test==min(test))
> or
> which(!is.na(match(test,min(test))))
> or
> match(test,min(test)) # to see the entire vector
> 
> However, my problem is as follows. I am not manually
> creating a vector to test for multiple minimums but
> rather doing a read.table on a file that contains the
> same data with other columns, etc. I then trim off the
> data from that matrix (there are several identical
> ways to do this):
> 
> unlist(myMatrix[1], use.name=FALSE)
> or
> myMatrix[,1]
> 
> But, running any of the tests listed above only
> returns the index of the last minimum. Both my manual
> vector and vector culled from the matrix appear to me
> to be identical (tested with is.vector, is.numeric,
> is.double, etc), but it will only every return the
> index of the second of the two minimums. Any help
> would be truly appreciated.
> 
> Thanks
> Barry
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list