title: For loop Spark dataframe
body: <p>I have a Dataframe <code>df</code> that has, among others, a column of <code>groupID</code>; that is, each observation belongs to a specific group. In total there are 8 groups. I would like to sample from each <code>groupID</code> a certain percent of observations (say, 20%). Here is my approach of doing this:</p>
  
  <pre><code>val sample_df = for ( i &lt;- Array.range(0,7) ) yield {  
               val sel_df = df.filter($"groupID"===i)  
               sel_df.sample(false,0.2,seed1)  
               }  
  </code></pre>
  
  <p>The result of this code is:</p>
  
  <pre><code>Array[org.apache.spark.sql.DataFrame] = Array([text: string, groupID: int], [text: string, groupID: int])
  </code></pre>
  
  <p>I applied <code>flatMap()</code> on <code>sample_df</code>, but I got an error:</p>
  
  <pre><code>val flat_df = sample_df.flatMap(x =&gt; x)
           &lt;console&gt;:59: error: type mismatch;
           found: org.apache.spark.sql.DataFrame
           required: scala.collection.GenTraversableOnce[?]
  </code></pre>
  
  <p>How can I get a sampled dataframe?</p>