[R] sparse vectors

Steve Lianoglou mailinglist.honeypot at gmail.com
Tue Sep 8 15:36:09 CEST 2009


Hi,

On Sep 8, 2009, at 9:06 AM, Robin Hankin wrote:

> Hi
>
> I deal with long vectors almost all of whose elements are zero.
> Typically, the length will be ~5e7 with ~100 nonzero elements.
>
> I want to deal with these objects using a sort of sparse
> vector.

Would using sparse matrices (from the Matrix or SparseM packages) be  
overkill?

-steve

>
> The problem is that I want to be able to 'add' two such
> vectors.
> Toy problem follows.  Suppose I have two such objects, 'a' and 'b':
>
>
>
> > a
> $index
> [1]    20   30 100000000
>
> $val
> [1] 2.2 3.3 4.4
>
>
>
> > b
> $index
> [1]   3  30
>
> $val
> [1] 0.1 0.1
>
> >
>
>
> What I want is the "sum" of these:
>
> > AplusB
> $index
> [1]    3   20   30 100000000
>
> $val
> [1]  0.1 2.2 3.4 4.4
>
> >
>
>
> See how the value for index=30 (being common to both) is 3.4
> (=3.3+0.1).   What's the best R idiom to achieve this?
>
>
>
> -- 
> Robin K. S. Hankin
> Uncertainty Analyst
> University of Cambridge
> 19 Silver Street
> Cambridge CB3 9EP
> 01223-764877
>
> ______________________________________________
> 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.

--
Steve Lianoglou
Graduate Student: Computational Systems Biology
   |  Memorial Sloan-Kettering Cancer Center
   |  Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact




More information about the R-help mailing list