title: Copy schema from one dataframe to another dataframe
body: <p>I'm trying to change the schema of an existing dataframe to the schema of another dataframe.</p>
  
  <p><strong>DataFrame 1:</strong></p>
  
  <pre><code>Column A | Column B | Column C | Column D
     "a"   |    1     |   2.0    |   300
     "b"   |    2     |   3.0    |   400
     "c"   |    3     |   4.0    |   500
  </code></pre>
  
  <p><strong>DataFrame 2:</strong></p>
  
  <pre><code>Column K | Column B | Column F
     "c"   |    4     |   5.0
     "b"   |    5     |   6.0
     "f"   |    6     |   7.0
  </code></pre>
  
  <p>So I want to apply the schema of the first dataframe on the second. So all the columns which are the same remain. The columns in dataframe 2 that are not in 1 get deleted. The others become "NULL".</p>
  
  <p><strong>Output</strong></p>
  
  <pre><code>Column A | Column B | Column C | Column D
   "NULL"  |    4     |   "NULL" |  "NULL"
   "NULL"  |    5     |   "NULL" |  "NULL"
   "NULL"  |    6     |   "NULL" |  "NULL"
  </code></pre>
  
  <p>So I came with a possible solution:</p>
  
  <pre><code>val schema = df1.schema
  val newRows: RDD[Row] = df2.map(row =&gt; {
    val values = row.schema.fields.map(s =&gt; {
      if(schema.fields.contains(s)){
        row.getAs(s.name).toString
      }else{
        "NULL"
      }
    })
    Row.fromSeq(values)
  })
  sqlContext.createDataFrame(newRows, schema)}
  </code></pre>
  
  <p>Now as you can see this will not work because the schema contains String, Int and Double. And all my rows have String values.</p>
  
  <p>This is where I'm stuck, is there a way to automatically convert the type of my values to the schema?</p>