[R] sort adjacency matrix
David Winsemius
dwinsemius at comcast.net
Mon Apr 6 23:55:28 CEST 2015
On Apr 6, 2015, at 11:15 AM, Bert Gunter wrote:
> Not quite, David.
>
> If I understand the OP's query, he wants the ties to be broken by the
> "lexicographic" order (with apologies if I have misused this term) of
> the 1's within the rows. Makes things a bit more interesting.
This should correct the problem:
> M <- as.matrix(m)
> M[ order(rowSums(M=="1"),
+ apply(M, 1, paste0, collapse=".") ,
+ decreasing=TRUE), ]
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 0 1 1 1 0 0 0 0 0 0
[2,] 0 0 0 1 1 0 1 0 0 0
[3,] 1 0 0 0 0 0 0 0 0 0
[4,] 1 0 0 0 0 0 0 0 0 0
[5,] 1 0 0 0 0 0 0 0 0 0
[6,] 0 1 0 0 0 0 0 0 0 0
[7,] 0 1 0 0 0 0 0 0 0 0
[8,] 0 0 0 0 1 0 0 0 0 0
[9,] 0 0 0 0 1 0 0 0 0 0
[10,] 0 0 0 0 0 0 0 0 0 0
The original matrix was sparse and I get this error message when attempting to use 'order' in the i-argument to the `[` method for dgCMatrix:
Error in m[order(rowSums(m == "1"), apply(m, 1, paste0, collapse = "."), :
error in evaluating the argument 'i' in selecting a method for function '[': Error: not-yet-implemented method for ==(<dgCMatrix>, <character>).
->> Ask the package authors to implement the missing feature.
HTH;
--
David.
> Have at it!
>
> Cheers,
> Bert
>
> Bert Gunter
> Genentech Nonclinical Biostatistics
> (650) 467-7374
>
> "Data is not information. Information is not knowledge. And knowledge
> is certainly not wisdom."
> Clifford Stoll
>
>
>
>
> On Mon, Apr 6, 2015 at 11:09 AM, David L Carlson <dcarlson at tamu.edu> wrote:
>> The answer depends on what kind of matrix/data frame you have. That is why we encourage people to use dput() to create a copy of the sample data in their email. Some combination of order() function the rowSums() function will probably get you what you want. For example,
>>
>> dat[order(rowSums(dat=="1"), decreasing=TRUE),]
>>
>> or
>>
>> dat[order(rowSums(dat), decreasing=TRUE),]
>>
>> or
>>
>> dat[order(rowSums(dat, na.rm=TRUE), decreasing=TRUE),]
>>
>> Note that the order is not unique since there are ties in the number of 1s.
>>
>> -------------------------------------
>> David L Carlson
>> Department of Anthropology
>> Texas A&M University
>> College Station, TX 77840-4352
>>
>>
>> -----Original Message-----
>> From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Ragia Ibrahim
>> Sent: Monday, April 6, 2015 12:18 PM
>> To: r-help at r-project.org
>> Subject: [R] sort adjacency matrix
>>
>> Dear group
>> i have the following matrix
>>
>> 1 . . 1 . . 1 . . . .
>> 2 . . . . . . 1 . . .
>> 3 1 . . . 1 . . 1 . 1
>> 4 . . . . . 1 . . . .
>> 5 . . 1 . . . . . . 1
>> 6 1 . . 1 . . . . 1 .
>> 7 . 1 . . . . . 1 . .
>> 8 . . 1 . . . 1 . . 1
>> 9 . . . . . 1 . . . 1
>> 10 . . 1 . 1 . . 1 1 .
>>
>> I want to sort it according to ones in each row ascending (where max number of ones first)
>>
>> to be as follow
>>
>> 3 1 . . . 1 . . 1 . 1
>> 10 . . 1 . 1 . . 1 1 .
>> 6 1 . . 1 . . . . 1 .8 . . 1 . . . 1 . . 11 . . 1 . . 1 . . . .5 . . 1 . . . . . . 17 . 1 . . . . . 1 . .9 . . . . . 1 . . . 12 . . . . . . 1 . . .4 . . . . . 1 . . . .
>>
>> how can I do this in R
>> thanks in advance
>>
>
David Winsemius
Alameda, CA, USA
More information about the R-help
mailing list