1

I tried to "generate" a spark query in this way

  def stdizedOperationmode(sqLContext: SQLContext,withrul: DataFrame): DataFrame = {
    // see http://spark.apache.org/docs/latest/sql-programming-guide.html
    import sqLContext.implicits._
    val AZ: Column  = lit(0.00000001)

    def opMode(id:Int): Column = {
      (column("s"+id) - coalesce(column("a"+id) / column("sd"+id), column("a"+id) / lit(AZ))).as("std"+id)
    }

    // add the 21 std<i> columns based on s<i> - (a<id>/sd<id>)
    val columns: IndexedSeq[Column] = 1 to 21 map(id => opMode(id))

    val withStd = withrul.select(columns:_*)

    withStd
  }

Question how do I add "all other columns" (*) idea: something like withrul.select('* :+ columns:_*)

1 Answer 1

4

You can try the following :

// add the 21 std<i> columns based on s<i> - (a<id>/sd<id>)

val columns: IndexedSeq[Column] = 1 to 21 map(id => opMode(id))

val selectAll: Array[Column] = (for {
  i <- withrul.columns
} yield withrul(i)) union columns.toSeq

val withStd = withrul.select(selectAll :_*)

The second line will yeild all the columns from withrul adding them with column as a Seq[Column]

You are not obliged to create a value to return it afterward, can replace the last 2 lines with :

withrul.select(selectAll : _*)
Sign up to request clarification or add additional context in comments.

1 Comment

I'd replace for...yield to union later with map. I'd also recommend (1 to 21).map(opMode).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.