[R] Return the matrix location of multiple entries

Dimitris Rizopoulos d.rizopoulos at erasmusmc.nl
Mon Jan 23 20:52:26 CET 2012


On 1/23/2012 8:40 PM, David Winsemius wrote:
>
> On Jan 23, 2012, at 2:30 PM, Petr Savicky wrote:
>
>> On Mon, Jan 23, 2012 at 01:08:03PM -0500, R. Michael Weylandt wrote:
>>> I'd do something like
>>>
>>> apply(subER, 1, function(x) which(x %in% sort(x)[1:4]))
>>>
>>> E.g.
>>>
>>> subER <- matrix(sample(100), 10)
>>
>> Hi.
>>
>> This is OK, if there are four smallest values, which
>> are different from the rest. For the first row in
>>
>> subER <- rbind(c(1, 2, 2, 3, 3, 3, 5, 6), 8:1)
>>
>> the function determines the bound 3 and returns the
>> indices of the 6 positions with 1, 2, 3 from the first
>> row. So, the result is not a matrix, but a list.
>>
>> apply(subER, 1, function(x) which(x %in% sort(x)[1:4]))
>>
>> [[1]]
>> [1] 1 2 3 4 5 6
>>
>> [[2]]
>> [1] 5 6 7 8
>>
>> The following solves ties by choosing the smaller index.
>>
>> apply(subER, 1, function(x) order(x)[1:4])
>>
>> [,1] [,2]
>> [1,] 1 8
>> [2,] 2 7
>> [3,] 3 6
>> [4,] 4 5
>>
>> If the indices should be ordered, then try the following
>>
>> apply(subER, 1, function(x) sort(order(x)[1:4]))
>>
>> [,1] [,2]
>> [1,] 1 5
>> [2,] 2 6
>> [3,] 3 7
>> [4,] 4 8
>
> And if only the lowest four instances were desired (in ascending order)
> then this would work (and I transposed to bring back to the original
> structure):
>
> t(apply(subER, 1, function(x) x[order(x)][1:4]))
> [,1] [,2] [,3] [,4]
> [1,] 1 2 2 3
> [2,] 1 2 3 4
>

Yet another solution is:

subER <- matrix(sample(100), 10)

matrix(subER[order(row(subER), subER)],
     ncol = ncol(subER), byrow = TRUE)[, 1:4]


Cheers,
Dimitris


-- 
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014
Web: http://www.erasmusmc.nl/biostatistiek/



More information about the R-help mailing list