title: Specify subset of elements in Spark RDD (Scala)
body: <p>My dataset is a <code>RDD[Array[String]]</code> with more than 140 columns. How can I select a subset of columns without hard-coding the column numbers <code>(.map(x =&gt; (x(0),x(3),x(6)...))</code>?</p>
  
  <p>This is what I've tried so far (with success):</p>
  
  <p><code>val peopleTups = people.map(x =&gt; x.split(",")).map(i =&gt; (i(0),i(1)))</code></p>
  
  <p>However, I need more than a few columns, and would like to avoid hard-coding them.</p>
  
  <p>This is what I've tried so far (that I think would be better, but has failed):</p>
  
  <pre><code>// Attempt 1
  val colIndices = [0,3,6,10,13]
  val peopleTups = people.map(x =&gt; x.split(",")).map(i =&gt; i(colIndices))
  
  // Error output from attempt 1:
  &lt;console&gt;:28: error: type mismatch;
   found   : List[Int]
   required: Int
         val peopleTups = people.map(x =&gt; x.split(",")).map(i =&gt; i(colIndices))
  
  // Attempt 2
  colIndices map peopleTups.lift
  
  // Attempt 3
  colIndices map peopleTups
  
  // Attempt 4
  colIndices.map(index =&gt; peopleTups.apply(index))
  </code></pre>
  
  <p>I found this question and tried it, but because I'm looking at an RDD instead of an array, it didn't work: <a href="https://stackoverflow.com/questions/29418877/how-can-i-select-a-non-sequential-subset-elements-from-an-array-using-scala-and">How can I select a non-sequential subset elements from an array using Scala and Spark?</a></p>