[R] Rows index/colProds

David Winsemius dwinsemius at comcast.net
Sun Aug 15 15:56:39 CEST 2010


On Aug 15, 2010, at 4:47 AM, pablo.andrade wrote:

>
> 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),

This is a theory untested in the absence of a reproducible example  
that reproduces the faulty behavior. You may want to look at what is  
offered by the help page:

?"["

... especially the drop argument. You can prevent single column  
matrices from being "vectorized" by using drop = FALSE.


> 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.
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list