Github user shivaram commented on a diff in the pull request:
https://github.com/apache/spark/pull/8266#discussion_r37322106
--- Diff: R/pkg/R/column.R ---
@@ -203,3 +203,49 @@ setMethod("%in%",
jc <- callJMethod(x@jc, "in", table)
return(column(jc))
})
+
+
+#' when
+#'
+#' Evaluates a list of conditions and returns one of multiple possible
result expressions.
+#' If otherwise is not defined at the end, null is returned for unmatched
conditions.
+#'
+#' @rdname column
+setMethod("when",
+ signature(x = "Column", y = "character", z = "ANY"),
+ function(x, y, z) {
+ condition <- y
+ value <- z
+ jc <- callJMethod(x@jc, "when", condition, value)
+ column(jc)
+ })
+
+#' otherwise
+#'
+#' Evaluates a list of conditions and returns one of multiple possible
result expressions.
+#' If otherwise is not defined at the end, null is returned for unmatched
conditions.
+#'
+#' @rdname column
+setMethod("otherwise",
+ signature(x = "Column", value = "ANY"),
+ function(x, value) {
+ value <- ifelse(class(value) == "Column", value@jc, value)
+ jc <- callJMethod(x@jc, "otherwise", value)
+ column(jc)
+ })
+
+#' otherwise
+#'
+#' This is an alias of `otherwise` as an infix operator.
+#'
+#' @rdname column
+#' @aliases otherwise
+#' @examples
+#' \dontrun{
+#' when(df$x > 1 & df$y < 10, 0) %otherwise% 1
+#' }
+setMethod("%otherwise%",
--- End diff --
This is an interesting idea, but I think the right primitive to implement
here is `ifelse` [1], which will capture both the if and the else clause in the
same function.
So I think we can remove the `%otherwise%` operator for now and we can
implement `ifelse` in a different PR if its too complicated to include in this
one.
[1] http://www.inside-r.org/r-doc/base/ifelse
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]