title: Spark - Create a DataFrame from a list of Rows generated in a loop
body: <p>I have a loop which generates rows in each iteration. My goal is to create a dataframe, with a given schema, that contents just those rows. I have in mind a set of steps to follow, but I am not able to add a new <code>Row</code> to a <code>List[Row]</code> in each loop iteration</p>
  
  <p>I am trying the following approach:</p>
  
  <pre><code>var listOfRows = List[Row]()
  
  val dfToExtractValues: DataFrame = ???
  
  dfToExtractValues.foreach { x =&gt; 
  
      //Not really important how to generate here the variables
      //So to simplify all the rows will have the same values
  
      var col1 = "firstCol"
      var col2 = "secondCol"  
      var col3 = "thirdCol"
  
      val newRow =  RowFactory.create(col1,col2,col3)
  
      //This step I am not able to do
      //listOfRows += newRow        -&gt; Just for strings
      //listOfRows.add(newRow)      -&gt; This add doesnt exist, it is a addString
      //listOfRows.aggregate(1)(newRow)       -&gt; This is not how aggreage works...
  }
  
  
  val rdd = sc.makeRDD[RDD](listOfRows)   
  
  val dfWithNewRows = sqlContext.createDataFrame(rdd, myOriginalDF.schema)
  </code></pre>
  
  <p>Can someone tell me what am I doing wrong, or what could I change in my approach to generate a dataframe from the rows I'm generating? </p>
  
  <p>Maybe there is a better way to collect the Rows instead of List[Row]. But then I need to convert that other type of collection into a dataframe.</p>