[R] high and lowest with names
"Dénes TÓTH"
tdenes at cogpsyphy.hu
Wed Oct 12 01:32:38 CEST 2011
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 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
> column names can easily be obtained from the dimnames attribute of the
> matrix.
>
> Note: This assumes that the object in question is a matrix, NOT a data
> frame, for which it would be slightly more complicated.
>
> -- Bert
>
>
> On Tue, Oct 11, 2011 at 3:06 PM, Carlos Ortega
> <cof at qualityexcellence.es>wrote:
>
>> 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 build a function
>> to
>> calculate everything and return a list.
>>
>>
>> Regards,
>> Carlos Ortega
>> www.qualityexcellence.es
>>
>> 2011/10/11 Ben qant <ccquant at gmail.com>
>>
>> > 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')
>> > > dat
>> > a b c d c
>> > z 12 7 6 2 10
>> > y 9 7 12 8 3
>> > x 5 8 7 28 12
>> > w 7 7 12 20 6
>> > v 15 13 5 9 1
>> >
>> > > #top 10
>> > > sort(dat,partial=n-9:n)[(n-9):n]
>> > [1] 9 10 12 12 12 12 13 15 20 28
>> > > # bottom 10
>> > > sort(dat,partial=1:10)[1:10]
>> > [1] 1 2 3 5 5 6 6 7 7 7
>> >
>> > ...except I need the rownames and colnames to go along for the ride
>> with
>> > the
>> > values...because of this, I am guessing the return value will need to
>> be
>> a
>> > list since all of the values have different row and col names (which
>> is
>> > fine).
>> >
>> > Regards,
>> >
>> > Ben
>> >
>> > [[alternative HTML version deleted]]
>> >
>> > ______________________________________________
>> > 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.
>> >
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> 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.
>>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>
More information about the R-help
mailing list