title: Spark scala how to count values in rows
body: <p>I am new to both Spark and Scala...and I have to read a data file and count the value that are contained in both columns and rows. The data set is structured like this: </p>
  
  <pre><code>0 0 2
  0 2 2
  0 2 0
  2 0 0
  0 0 0
  0 1 0
  </code></pre>
  
  <p>In order to count the number of "2" in each column:</p>
  
  <p>I imported the file:</p>
  
  <pre><code>val ip = sc.textFile("/home/../data-scala.txt")
  </code></pre>
  
  <p>I created an array to save my results</p>
  
  <pre><code>var ArrayCol = Array.ofDim[Long](3)
  
  val cols = ip.map(line =&gt; line.split(" "))
  
  for (i &lt;- 0 to 2) {
      ArrayCol(i) = cols.map(col =&gt; col(i)).filter(_.contains("2")).count()
  }
  </code></pre>
  
  <p>and I counted the number of "2" contained in each column.</p>
  
  <p>Now I would like to do the same for each row. Do you have any suggestion?</p>