-1

Matrix containing 100 columns and 10000 rows

I want my answer to be formatted like. Rows containing indices of top 5 values in descending order to be stored in another matrix:

R1:6, 3, 7, 5, 10
R2:7, 1, 2, 3, 4
R3:3, 1, 2, 4, 5

and so on.

Unheilig
  • 16,196
  • 193
  • 68
  • 98
  • Welcome to SO. First of all you should read [here](http://stackoverflow.com/help/how-to-ask) about how to ask a good question; a good question has better changes to be solved and you to receive help. On the other hand a read of [this](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) is also good. It explains how to create a reproducible example in R. Help users to help you by providing a piece of your data (not an useless picture of it!!) a desired output and things you have tried so far. – SabDeM Oct 24 '15 at 18:41
  • Please use `dput` to show the dataset. It is not so easy to convert an image file to text and extract the values.. Though, I tried some online conversions, the format completely changed, then have to do some extra manipulations to get it right. – akrun Oct 24 '15 at 19:31
  • thanks for the feedback akrun.I will surely keep that in mind next time.Sorry for any inconvenience caused – NAMAN SHARMA Oct 28 '15 at 13:10

1 Answers1

1

We can loop over the rows using apply with MARGIN=1, order the elements, select the first 5 element and transpose the matrix.

t(apply(m1, 1, FUN= function(x) order(-x)[1:5]))
#    [,1] [,2] [,3] [,4] [,5]
#[1,]    6    3    7    5   10
#[2,]    7    1    2    3    4
#[3,]    3    1    2    4    5

NOTE: Here, I am using only the first 3 rows of the image data provided by the OP.

data

m1 <- structure(c(0.001371742, 0.058823529, 0.428571429, 0, 0, 0,
0.10973937, 
0, 0.71428571, 0, 0, 0, 0.04115226, 0, 0, 0.25377229, 0, 0, 0.10013717, 
0.91176471, 0, 0, 0, 0, 0, 0, 0, 0.006858711, 0, 0), 
.Dim = c(3L, 
10L), .Dimnames = list(NULL, c("V1", "V2", "V3", "V4", "V5", 
"V6", "V7", "V8", "V9", "V10")))
Community
  • 1
  • 1
akrun
  • 874,273
  • 37
  • 540
  • 662