title: Casting type of columns in a dataframe
body: <p>My Spark program needs to read a file which contains a matrix of integers. Columns are separated with ",". Number of columns is not the same each time I run the program.</p>
  
  <p>I read the file as a dataframe:</p>
  
  <p><code>var df = spark.read.csv(originalPath);</code></p>
  
  <p>but when I print schema it gives me all the columns as Strings.</p>
  
  <p>I convert all columns to Integers as below but after that when I print the schema of df again, columns are still Strings.</p>
  
  <pre><code>df.columns.foreach(x =&gt; df.withColumn(x + "_new", df.col(x).cast(IntegerType))
  .drop(x).withColumnRenamed(x + "_new", x));
  </code></pre>
  
  <p>I appreciate any help to solve the issue of casting.</p>
  
  <p>Thanks.</p>