Skip to content

Commit

Permalink
HEX-2031: safely overload ifelse
Browse files Browse the repository at this point in the history
  • Loading branch information
spennihana committed May 3, 2015
1 parent 59fbbae commit b90ee8c
Showing 1 changed file with 40 additions and 33 deletions.
73 changes: 40 additions & 33 deletions R/h2o-package/R/Classes.R
Original file line number Diff line number Diff line change
Expand Up @@ -1815,41 +1815,48 @@ function(test, yes, no, type) {
}
}

ifelse<-
function (test, yes, no)
{
if (.check.ifelse.conditions(test, yes, no, "test")) {
if (is.logical(yes)) yes <- as.numeric(yes)
if (is.logical(no)) no <- as.numeric(no)
return(.h2o.__multop2("ifelse", test, yes, no))

} else if ( class(yes) == "H2OParsedData" && class(test) == "logical") {
if (is.logical(yes)) yes <- as.numeric(yes)
if (is.logical(no)) no <- as.numeric(no)
return(.h2o.__multop2("ifelse", as.numeric(test), yes, no))

} else if (class(no) == "H2OParsedData" && class(test) == "logical") {
if (is.logical(yes)) yes <- as.numeric(yes)
if (is.logical(no)) no <- as.numeric(no)
return(.h2o.__multop2("ifelse", as.numeric(test), yes, no))
}
if (is.atomic(test))
storage.mode(test) <- "logical"
else test <- if (isS4(test))
as(test, "logical")
else as.logical(test)
ans <- test
ok <- !(nas <- is.na(test))
if (any(test[ok]))
ans[test & ok] <- rep(yes, length.out = length(ans))[test &
ok]
if (any(!test[ok]))
ans[!test & ok] <- rep(no, length.out = length(ans))[!test &
ok]
ans[nas] <- NA
ans
setMethod("ifelse", signature(test="H2OParsedData", yes="ANY", no="ANY"), function (test, yes, no) {
.h2o.ifelse(test,yes,no)
}

setMethod("ifelse", signature(test="ANY",yes="H2OFrame", no="H2OFrame"), function(test,yes,no)
.h2o.ifelse(test,yes,no)
})

.h2o.ifelse <- function(test,yes,no) {
if (.check.ifelse.conditions(test, yes, no, "test")) {
if (is.logical(yes)) yes <- as.numeric(yes)
if (is.logical(no)) no <- as.numeric(no)
return(.h2o.__multop2("ifelse", test, yes, no))

} else if ( class(yes) == "H2OParsedData" && class(test) == "logical") {
if (is.logical(yes)) yes <- as.numeric(yes)
if (is.logical(no)) no <- as.numeric(no)
return(.h2o.__multop2("ifelse", as.numeric(test), yes, no))

} else if (class(no) == "H2OParsedData" && class(test) == "logical") {
if (is.logical(yes)) yes <- as.numeric(yes)
if (is.logical(no)) no <- as.numeric(no)
return(.h2o.__multop2("ifelse", as.numeric(test), yes, no))
}
if (is.atomic(test))
storage.mode(test) <- "logical"
else test <- if (isS4(test))
as(test, "logical")
else as.logical(test)
ans <- test
ok <- !(nas <- is.na(test))
if (any(test[ok]))
ans[test & ok] <- rep(yes, length.out = length(ans))[test &
ok]
if (any(!test[ok]))
ans[!test & ok] <- rep(no, length.out = length(ans))[!test &
ok]
ans[nas] <- NA
ans
}


#.getDomainMapping2 <- function(l, s = "") {
# if (is.list(l)) {
# return( .getDomainMapping2( l[[length(l)]], s))
Expand Down

0 comments on commit b90ee8c

Please sign in to comment.