[R] sparse vectors

(Ted Harding) Ted.Harding at manchester.ac.uk
Tue Sep 8 15:42:57 CEST 2009


On 08-Sep-09 13:06:28, 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.
> 
> 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?

I don't know about "the best", Robin, but how about something like:

  indices <- sort(unique(c(a$index,b$index)))
  N       <- length(indices)
  values  <- NULL
  for(i in indices){
    if(i %in% a$index){A <- a$val[a$index==i]} else A <- 0
    if(i %in% b$index){B <- b$val[b$index==i]} else B <- 0
    values <- c(values,A+B)
  }
  AplusB <- list(index=indices,val=values)

## Test:
  a<-list(index=c(20,30,100000000),val=c(2.2,3.3,4.4))
  b<-list(index=c(3,30),val=c(0.1, 0.1))
  indices <- sort(unique(c(a$index,b$index)))
  N       <- length(indices)
  values  <- NULL
  for(i in indices){
    if(i %in% a$index){A <- a$val[a$index==i]} else A <- 0
    if(i %in% b$index){B <- b$val[b$index==i]} else B <- 0
    values <- c(values,A+B)
  }
  AplusB <- list(index=indices,val=values)
  AplusB
  # $index
  # [1] 3e+00 2e+01 3e+01 1e+08
  # $val
  # [1] 0.1 2.2 3.4 4.4

Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 08-Sep-09                                       Time: 14:42:53
------------------------------ XFMail ------------------------------




More information about the R-help mailing list