Search code examples
rapache-sparkapache-spark-sqlsparkr

SparkR: How to merge multiple "when"/"otherwise" multiple conditions


I am trying to create a new column in base a multiple conditions, but I saw that I can't use multiple when clauses with only one otherwise and I was constrained to use somthing like below:

 test1 <- test %>%  withColumn("newCol1", otherwise(when(column("oldCol1") == 1, lit("one")), 
                                                    otherwise(when(column("oldCol1") == 2,lit("two")),
                                                              lit("other")))) %>%
  select(column("oldCol1"), column("newCol1"))

that gives me expected result:

     oldCol1 newCol1
1          1     one
2          1     one
3          2     two
4          4   other
5          4   other
6          1     one

Is there a clearer way to use WHEN function in SparkR?


Solution

  • You can try coalesce-ing the when statements:

    test1 <- test %>% withColumn(
        "newCol1", 
        coalesce(
            when(column("oldCol1") == 1, lit("one")),
            when(column("oldCol1") == 2, lit("two")),
            lit("other")
        )
    ) %>% select(column("oldCol1"), column("newCol1"))