title: How to declare a sparse Vector in Spark with Scala?
body: <p>I'm trying to create a sparse Vector (the <a href="https://spark.apache.org/docs/1.5.0/api/scala/index.html#org.apache.spark.mllib.linalg.Vectors$" rel="nofollow">mllib.linalg.Vectors</a> class, not the default one) but I can't understand how to use Seq. I have a small test file with three numbers/line, which I convert to an rdd, split the text in doubles and then group the lines by their first column.</p>
  
  <p>Test file</p>
  
  <pre><code>1 2 4
  1 3 5    
  1 4 8    
  2 7 5    
  2 8 4    
  2 9 10
  </code></pre>
  
  <p>Code</p>
  
  <pre><code>val data = sc.textFile("/home/savvas/DWDM/test.txt")
  val data2 = data.map(s =&gt; Vectors.dense(s.split(' ').map(_.toDouble)))
  val grouped = data2.groupBy( _(0) )
  </code></pre>
  
  <p>This results in <code>grouped</code> having these values</p>
  
  <pre><code>(2.0,CompactBuffer([2.0,7.0,5.0], [2.0,8.0,4.0], [2.0,9.0,10.0]))
  (1.0,CompactBuffer([1.0,2.0,4.0], [1.0,3.0,5.0], [1.0,4.0,8.0]))
  </code></pre>
  
  <p>But I can't seem to figure out the next step. I need to take each line of <code>grouped</code> and create a vector for it, so that each line of the new RDD has a vector with the third value of the <code>CompactBuffer</code> in the index specified by the second value. In short, what I mean is that I want my data in the example like this.</p>
  
  <pre><code>[0, 0, 0, 0, 0, 0, 5.0, 4.0, 10.0, 0]
  [0, 4.0, 5.0, 8.0, 0, 0, 0, 0, 0, 0]
  </code></pre>
  
  <p>I know I need to use a sparse vector, and that there are three ways to construct it. I've tried using a Seq with a tuple2(index, value) , but I cannot understand how to create such a Seq.</p>