Re: [R] high and lowest with names

2011-10-13 Thread Ben qant
Besides being a much better solution, it displays ties (which I see as a benefit). For example, if I ask for 5 I get 8 for top values since 12 occurs 3 times. Here is the same thing David posted with slight mods to generalize it a bit for cnt: x <- swiss$Education[1:25] dat = matrix(x,5,5) colnam

Re: [R] high and lowest with names

2011-10-13 Thread David Winsemius
On Oct 13, 2011, at 10:42 AM, Ben qant wrote: Here is a more R'sh solution (speed unknown). Really? The intermediate, potentially large, objects seem to be proliferating. Courtesy of Mark Leeds (I modified it a bit to generalize it for a cnt input and get min and max). Again, getting

Re: [R] high and lowest with names

2011-10-13 Thread Ben qant
Here is a more R'sh solution (speed unknown). Courtesy of Mark Leeds (I modified it a bit to generalize it for a cnt input and get min and max). Again, getting cnt highest and lowest values in the entire matrix and display the data point row and column names with each: > x <- swiss$Education[1:25]

Re: [R] high and lowest with names

2011-10-12 Thread Ben qant
Hello, This is my solution. This is pretty fast (tested with a larger data set)! If you have a more elegant way to do it (of similar speed), please reply. Thanks for the help! ## get highest and lowest values and names of a matrix # create sample data x <- swiss$Education[1:25] da

Re: [R] high and lowest with names

2011-10-11 Thread Dénes TÓTH
which.max is even faster: dims <- c(1000,1000) tt <- array(rnorm(prod(dims)),dims) # which system.time( replicate(100, which(tt==max(tt), arr.ind=TRUE)) ) # which.max (& arrayInd) system.time( replicate(100, arrayInd(which.max(tt), dims)) ) Best, Denes > But it's simpler and probably faster to

Re: [R] high and lowest with names

2011-10-11 Thread Bert Gunter
But it's simpler and probably faster to use R's built-in capabilities. ?which ## note the arr.ind argument!) As an example: test <- matrix(rnorm(24), nr = 4) which(test==max(test), arr.ind=TRUE) row col [1,] 2 6 So this gives the row and column indices of the max, from which row and col

Re: [R] high and lowest with names

2011-10-11 Thread Carlos Ortega
Hi, With this code you can find row and col names for the largest value applied to your example: r.m.tmp<-apply(dat,1,max) r.max<-names(r.m.tmp)[r.m.tmp==max(r.m.tmp)] c.m.tmp<-apply(dat,2,max) c.max<-names(c.m.tmp)[c.m.tmp==max(c.m.tmp)] It's inmediate how to get the same for the smallest and

[R] high and lowest with names

2011-10-11 Thread Ben qant
Hello, I'm looking to get the values, row names and column names of the largest and smallest values in a matrix. Example (except is does not include the names): > x <- swiss$Education[1:25] > dat = matrix(x,5,5) > colnames(dat) = c('a','b','c','d','c') > rownames(dat) = c('z','y','x','w','v') >