aboutsummaryrefslogtreecommitdiff
path: root/R/pkg
diff options
context:
space:
mode:
authorYu ISHIKAWA <yuu.ishikawa@gmail.com>2015-08-19 12:39:37 -0700
committerShivaram Venkataraman <shivaram@cs.berkeley.edu>2015-08-19 12:39:37 -0700
commitd898c33f774b9a3db2fb6aa8f0cb2c2ac6004b58 (patch)
treec97c1209ff5b424af6795584c28f2b1ce058904e /R/pkg
parent28a98464ea65aa7b35e24fca5ddaa60c2e5d53ee (diff)
downloadspark-d898c33f774b9a3db2fb6aa8f0cb2c2ac6004b58.tar.gz
spark-d898c33f774b9a3db2fb6aa8f0cb2c2ac6004b58.tar.bz2
spark-d898c33f774b9a3db2fb6aa8f0cb2c2ac6004b58.zip
[SPARK-10106] [SPARKR] Add `ifelse` Column function to SparkR
### JIRA [[SPARK-10106] Add `ifelse` Column function to SparkR - ASF JIRA](https://issues.apache.org/jira/browse/SPARK-10106) Author: Yu ISHIKAWA <yuu.ishikawa@gmail.com> Closes #8303 from yu-iskw/SPARK-10106.
Diffstat (limited to 'R/pkg')
-rw-r--r--R/pkg/NAMESPACE1
-rw-r--r--R/pkg/R/functions.R19
-rw-r--r--R/pkg/inst/tests/test_sparkSQL.R3
3 files changed, 22 insertions, 1 deletions
diff --git a/R/pkg/NAMESPACE b/R/pkg/NAMESPACE
index 111a2dc30d..3e5c89d779 100644
--- a/R/pkg/NAMESPACE
+++ b/R/pkg/NAMESPACE
@@ -135,6 +135,7 @@ exportMethods("abs",
"hex",
"hour",
"hypot",
+ "ifelse",
"initcap",
"instr",
"isNaN",
diff --git a/R/pkg/R/functions.R b/R/pkg/R/functions.R
index 5dba0887d1..b5879bd9ad 100644
--- a/R/pkg/R/functions.R
+++ b/R/pkg/R/functions.R
@@ -594,3 +594,22 @@ setMethod("when", signature(condition = "Column", value = "ANY"),
jc <- callJStatic("org.apache.spark.sql.functions", "when", condition, value)
column(jc)
})
+
+#' ifelse
+#'
+#' Evaluates a list of conditions and returns `yes` if the conditions are satisfied.
+#' Otherwise `no` is returned for unmatched conditions.
+#'
+#' @rdname column
+setMethod("ifelse",
+ signature(test = "Column", yes = "ANY", no = "ANY"),
+ function(test, yes, no) {
+ test <- test@jc
+ yes <- ifelse(class(yes) == "Column", yes@jc, yes)
+ no <- ifelse(class(no) == "Column", no@jc, no)
+ jc <- callJMethod(callJStatic("org.apache.spark.sql.functions",
+ "when",
+ test, yes),
+ "otherwise", no)
+ column(jc)
+ })
diff --git a/R/pkg/inst/tests/test_sparkSQL.R b/R/pkg/inst/tests/test_sparkSQL.R
index 670017ed34..556b8c5447 100644
--- a/R/pkg/inst/tests/test_sparkSQL.R
+++ b/R/pkg/inst/tests/test_sparkSQL.R
@@ -813,11 +813,12 @@ test_that("greatest() and least() on a DataFrame", {
expect_equal(collect(select(df, least(df$a, df$b)))[, 1], c(1, 3))
})
-test_that("when() and otherwise() on a DataFrame", {
+test_that("when(), otherwise() and ifelse() on a DataFrame", {
l <- list(list(a = 1, b = 2), list(a = 3, b = 4))
df <- createDataFrame(sqlContext, l)
expect_equal(collect(select(df, when(df$a > 1 & df$b > 2, 1)))[, 1], c(NA, 1))
expect_equal(collect(select(df, otherwise(when(df$a > 1, 1), 0)))[, 1], c(0, 1))
+ expect_equal(collect(select(df, ifelse(df$a > 1 & df$b > 2, 0, 1)))[, 1], c(1, 0))
})
test_that("group by", {