dimanche 19 février 2017

how to randomly select a fixed number of entries in a distance matrix using an index in R

I've been searching SO for a way on how randomly select a fixed number of entries in a distance matrix using an index but haven't found a solution.

I have a matrix (actually it has more than 10,000 rows and cols) like:

        [,1]    [,2]    [,3]    [,4]    [,5]    [,6]    [,7]    [,8]    [,9]    [,10]
[,1]    NA      1       2       3       2       1       2       3       2       1
[,2]    1       0       0.3     0.29    0.21    0.3     0.14    0.25    0.14    0.09
[,3]    2       0.3     0       0.38    0.3     0.39    0.32    0.36    0.14    0.41
[,4]    3       0.29    0.38    0       0.32    0.3     0.25    0.21    0.13    0.21
[,5]    2       0.21    0.3     0.32    0       0.3     0.29    0.23    0.16    0.2
[,6]    1       0.3     0.39    0.3     0.3     0       0.2     0.29    0.14    0.2
[,7]    2       0.14    0.32    0.25    0.29    0.2     0       0.18    0.13    0.25
[,8]    3       0.25    0.36    0.21    0.23    0.29    0.18    0       0.25    0.21
[,9]    2       0.14    0.14    0.13    0.16    0.14    0.13    0.25    0       0.16
[,10]   1       0.09    0.41    0.21    0.2     0.2     0.25    0.21    0.16    0

I am looking for a way to random select a fixed number of rows/cols (e.g.; 2) from this matrix on the basis of the index in row/col 1.

I should obtain a matrix like:

        [,1]    [,2]    [,3]    [,4]    [,5]    [,6]    [,7]
[,1]    NA      1       2       3       1       2       3
[,2]    1       0       0.3     0.29    0.3     0.14    0.25
[,3]    2       0.3     0       0.38    0.39    0.32    0.36
[,4]    3       0.29    0.38    0       0.3     0.25    0.21
[,5]    1       0.3     0.39    0.3     0       0.2     0.29
[,6]    2       0.14    0.32    0.25    0.2     0       0.18
[,7]    3       0.25    0.36    0.21    0.29    0.18    0

Thanks in advance for your help.




Aucun commentaire:

Enregistrer un commentaire