[R] Matrix-list table conversion+nrwos with specefic values.
Jeff Newmiller
jdnewmil at dcn.davis.ca.us
Sat Apr 29 21:11:08 CEST 2017
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
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list