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
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
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]
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
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
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
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
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')
>
8 matches
Mail list logo