[R] Rows index/colProds

pablo.andrade pablo.andrade at gmail.com
Sun Aug 15 10:47:14 CEST 2010


Hi,

Is there any function to replace colProds that finds column-wise products of
a matrix?

Or is there any other function that would give a better solution this
problem ?

I have a matrix and an array, example:

> a
                     GSM1.CEL  GSM2.CEL  GSM1.CEL
10000_at               1             3             1
10001_at               3             3             3

> b
10000_at 10001_at 
       1        3 

I want to find the number of occurrences of this array within the matrix
(which in this case is 2).

> a==b
gives me:
                     GSM1.CEL  GSM2.CEL  GSM1.CEL
10000_at          TRUE         FALSE          TRUE
10001_at          TRUE          TRUE          TRUE

What I have done is :

> colProds(a==b)
                     GSM1.CEL  GSM2.CEL  GSM1.CEL
                            1           0             1

> sum(colProds(a==b)) 
2                    -----> that gives the value of 2 that I want.

The problem is: if the matrix "a" is actually a vector (matrix 1xN), the
function "colProds" multiply the elements of the row as if it was a column:

> a
10000_at 
       1 
> b
       GSM1.CEL  GSM2.CEL  GSM1.CEL
            1             3             1 
> a==b

       GSM1.CEL  GSM2.CEL  GSM1.CEL
         TRUE         FALSE          TRUE 

> colProds(a==b)
[1] 0


I could use a if length(b==1) find sum(a==b) instead of sum(colProds(a==b)),
but I would have to use this condition many time through the code, so I
would like to know if there a better way to do that.

Thank you very much.
Pablo.


-- 
View this message in context: http://r.789695.n4.nabble.com/Rows-index-colProds-tp2325771p2325771.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list