title: Calculate mean for several columns in Spark scala
body: <p>I'm looking for a way to calculate some statistic e.g. mean over several selected columns in Spark using Scala. Given that <code>data</code> object is my Spark <code>DataFrame</code>, it's easy to calculate a mean for one column only e.g.</p>
  
  <pre><code>data.agg(avg("var1") as "mean var1").show
  </code></pre>
  
  <p>Also, we can easily calculate a mean cross-tabulated by values of some other columns e.g.:</p>
  
  <pre><code>data.groupBy("category").agg(avg("var1") as "mean_var1").show
  </code></pre>
  
  <p>But how can we calculate a mean for a List of columns in a DataFrame? I tried running something like this, but it didn't work:</p>
  
  <pre><code>scala&gt; data.select("var1", "var2").mean().show
  &lt;console&gt;:44: error: value mean is not a member of org.apache.spark.sql.DataFrame
         data.select("var1", "var2").mean().show
                                     ^
  </code></pre>