# [R] Matrix-list table conversion+nrwos with specefic values.

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Sun Apr 30 00:15:09 CEST 2017

```Please use reply-all to make sure the mailing list is included so others can learn from the discussion.

Yes, you can use row and column names. Have you tried it?
--
Sent from my phone. Please excuse my brevity.

On April 29, 2017 2:44:06 PM PDT, abo dalash <abo_dlsh at hotmail.com> wrote:
>Many thanks Jeff for your rapid response.
>
>
>Reg. the first task, I will discuss this later with you as I have faced
>some issues.
>
>
>Your guidance reg. Matrix-Table conversion has produced the table I
>want.
>
>thank you so much. My matrix is of size 120*120 and the headers of the
>columns and rows contain the same list of drug names. The table I
>produced
>
>contains the number of rows and number of columns in the matrix under
>
>r and c columns in my produced table. Is it possible to have names of
>drugs
>
>in my table instead of the number of row or number of column of the
>matrix.
>
>Please let me know if this is not clear.
>
>
>Many thanks
>
>
>________________________________
>From: Jeff Newmiller <jdnewmil at dcn.davis.ca.us>
>Sent: 29 April 2017 10:11 PM
>To: r-help at r-project.org; Bert Gunter; abo dalash; R-help
>Subject: Re: [R] Matrix-list table conversion+nrwos with specefic
>values.
>
>Break it down. If you have a scalar value val and you want to know if
>it is in a vector vec, using val==vec gets you a logical vector as long
>as vec. You can use val %in% vec and you get a logical vector as long
>as val (e.g. 1). If val is a vector of, say, length 2, then you will
>get a length 2 logical vector. In your example, c(566,23) %in% c(123,
>566, 235) would be c( TRUE, TRUE ). Since you want both of these
>elements to be TRUE,  you can use the all() function as in all(
>c(566,23) %in% c(123, 566, 235) ). So use the apply function to examine
>each row as a vector and you have it:
>
>apply( mat, 1, function(v) { all( c( 566,235) %in% v ) } )
>
>Note that this only works if your data are integers (see FAQ 7.31).
>
>Re the matrix to table... use expand.grid and matrix indexing.
>
>tbl <- expand.grid( r = seq.int( nrow( mat ) )
>                  , c = seq.int( ncol( mat ) ) )
>tbl\$val <- mat[ as.matrix( tbl[ , c( "r","c" ) ] ) ]
>tbl[ order( tbl\$val, decreasing=TRUE), ]
>
>--
>Sent from my phone. Please excuse my brevity.
>
>On April 29, 2017 9:53:08 AM PDT, Bert Gunter <bgunter.4567 at gmail.com>
>wrote:
>>I am not a private (or free!) consultant. Post to the r-help if your
>>question concerns R.
>>
>>-- Bert
>>
>>Bert Gunter
>>
>>
>>
>>On Sat, Apr 29, 2017 at 8:51 AM, abo dalash <abo_dlsh at hotmail.com>
>>wrote:
>>> Hi dear Bert
>>>
>>>
>>> I'm trying to identify number of rows containing 2 specific values.
>>>
>>> I tried : which(mydata == 566,235), but this returns logical values
>>for all
>>> rows and any T in a certain row indicates the existence of one of
>>these
>>> values but what I need to know is only number of rows in my data set
>>with
>>> these 2 particular values considering these two values
>>>
>>> as one pair per row. For example :
>>>
>>>
>>> 1          123   566    235
>>>
>>> 2          443    54      566
>>>
>>> 3          566    44      235
>>>
>>>
>>> here number of rows with the values 566&235 is 2 which are
>>>
>>> rows 1 & 3. Row 2 has only 566 so it should not be included in
>>>
>>> our calculation.
>>>
>>>
>>> I also have a large matrix and wanted to convert it into a table so
>I
>>can
>>>
>>> easily identify the combination with higher frequencies.
>>>
>>>
>>> The matrix looks like this:
>>>
>>>
>>>                     x      y      z
>>>
>>> x                  0      5       67
>>>
>>> y                  na    0      23
>>>
>>> z                   na   na      0
>>>
>>>
>>> and I would like to convert this into a table arranged with
>>>
>>> higher values first like this :
>>>
>>> x       z       67
>>>
>>> y       z       23
>>>
>>> x       y        5
>>>
>>> x       x        0
>>>
>>> y       y        0
>>>
>>> z        z        0
>>>
>>> y        x        na
>>>
>>> z        x        na
>>>
>>> z        y        na
>>>
>>>
>>> Is there a simple function to perform this conversion with some
>>explanation
>>> about the Syntax if you don't mind?
>>>
>>>
>>> Regards
>>
>>______________________________________________
>>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>https://stat.ethz.ch/mailman/listinfo/r-help
>
>R-help -- Main R Mailing List: Primary help - Homepage -
>SfS<https://stat.ethz.ch/mailman/listinfo/r-help>
>stat.ethz.ch
>The main R mailing list, for announcements about the development of R
>and solutions using R ...
>
>
>
>>http://www.R-project.org/posting-guide.html
>>and provide commented, minimal, self-contained, reproducible code.

```