title: How can I change column types in Spark SQL's DataFrame?
body: <p>Suppose I'm doing something like:</p>
  
  <pre><code>val df = sqlContext.load("com.databricks.spark.csv", Map("path" -&gt; "cars.csv", "header" -&gt; "true"))
  df.printSchema()
  
  root
   |-- year: string (nullable = true)
   |-- make: string (nullable = true)
   |-- model: string (nullable = true)
   |-- comment: string (nullable = true)
   |-- blank: string (nullable = true)
  
  df.show()
  year make  model comment              blank
  2012 Tesla S     No comment
  1997 Ford  E350  Go get one now th...
  </code></pre>
  
  <p>But I really wanted the <code>year</code> as <code>Int</code> (and perhaps transform some other columns).</p>
  
  <p>The best I could come up with was</p>
  
  <pre><code>df.withColumn("year2", 'year.cast("Int")).select('year2 as 'year, 'make, 'model, 'comment, 'blank)
  org.apache.spark.sql.DataFrame = [year: int, make: string, model: string, comment: string, blank: string]
  </code></pre>
  
  <p>which is a bit convoluted.</p>
  
  <p>I'm coming from R, and I'm used to being able to write, e.g.</p>
  
  <pre><code>df2 &lt;- df %&gt;%
     mutate(year = year %&gt;% as.integer,
            make = make %&gt;% toupper)
  </code></pre>
  
  <p>I'm likely missing something, since there should be a better way to do this in Spark/Scala...</p>