From 69515ebe5915f6266a840ca0914d36bfd9bfed98 Mon Sep 17 00:00:00 2001 From: SpencerA Date: Tue, 17 Mar 2015 00:25:10 -0700 Subject: [PATCH] PUB-1153: set a level on a factor col --- R/h2o-package/R/Classes.R | 8 +++++++ R/h2o-package/man/h2o.setLevel.Rd | 22 +++++++++++++++++++ src/main/java/water/exec/ASTOp.java | 33 +++++++++++++++++++++++++++-- 3 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 R/h2o-package/man/h2o.setLevel.Rd diff --git a/R/h2o-package/R/Classes.R b/R/h2o-package/R/Classes.R index bcd21de695..504f901225 100755 --- a/R/h2o-package/R/Classes.R +++ b/R/h2o-package/R/Classes.R @@ -1648,6 +1648,14 @@ h2o.sub <- function(pattern, replacement, x, ignore.case = FALSE) { return(res) } +h2o.setLevel <- function(x, level) { + expr <- paste("setLevel(", paste(x@key, deparse(level), sep = ","), ")", sep = "") + res <- .h2o.__exec2(x@h2o, expr) + res <- .h2o.exec2(res$dest_key, h2o = x@h2o, res$dest_key) + res@logic <- FALSE + res +} + trim <- function(x) { if (!inherits(x, "H2OParsedData")) stop("x must be an H2OParsedData object") .h2o.__unop2("trim", x) diff --git a/R/h2o-package/man/h2o.setLevel.Rd b/R/h2o-package/man/h2o.setLevel.Rd new file mode 100644 index 0000000000..c0b80bd8e9 --- /dev/null +++ b/R/h2o-package/man/h2o.setLevel.Rd @@ -0,0 +1,22 @@ +\name{h2o.setLevel} +\alias{h2o.setLevel} +\title{Pattern Replacement} +\description{ \code{h2o.setLevel}, a method to set a factor column to one of the levels.} +\usage{h2o.setLevel(x, level)} +\arguments{ + \item{x}{An \code{\linkS4class{H2OParsedData}} object with a single factor column.} + \item{level}{The level at which the column will be set.} + } +\details{ + Replace all other occurrences with `level` in a factor column. +} +\value{ +An object of class "H2OParsedData". +} + +\examples{ +library(h2o) +localH2O <- h2o.init(ip = "localhost", port = 54321, startH2O = TRUE) +hex <- as.h2o(localH2O , iris) +hex$Species <- h2o.setLevel(hex$Species, "versicolor") +} diff --git a/src/main/java/water/exec/ASTOp.java b/src/main/java/water/exec/ASTOp.java index 423de40a2e..648987cb4e 100644 --- a/src/main/java/water/exec/ASTOp.java +++ b/src/main/java/water/exec/ASTOp.java @@ -21,8 +21,6 @@ import java.util.*; import java.util.concurrent.atomic.AtomicInteger; -import static water.util.Utils.seq; - /** Parse a generic R string and build an AST, in the context of an H2O Cloud * @author cliffc@0xdata.com */ @@ -183,6 +181,7 @@ public abstract class ASTOp extends AST { putPrefix(new ASTToLower()); putPrefix(new ASTToUpper()); putPrefix(new ASTGSub()); + putPrefix(new ASTSetLevel()); putPrefix(new ASTStrSub()); putPrefix(new ASTRevalue()); putPrefix(new ASTWhich()); @@ -994,6 +993,36 @@ class ASTGSub extends ASTOp { } } +class ASTSetLevel extends ASTOp { + ASTSetLevel() { super(new String[]{"setLevel", "x", "level"}, + new Type[]{Type.ARY, Type.ARY, Type.STR}, + OPF_PREFIX, + OPP_PREFIX, OPA_RIGHT); } + @Override String opStr() { return "setLevel"; } + @Override ASTOp make() { return new ASTSetLevel(); } + @Override void apply(Env env, int argcnt, ASTApply apply) { + final String level = env.popStr(); + String skey = env.key(); + Frame fr = env.popAry(); + if (fr.numCols() != 1) throw new IllegalArgumentException("setLevel works on a single column at a time."); + String[] doms = fr.anyVec().domain().clone(); + if( doms == null ) + throw new IllegalArgumentException("Cannot set the level on a non-factor column!"); + final int idx = Arrays.asList(doms).indexOf(level); + if (idx == -1) + throw new IllegalArgumentException("Did not find level `" + level + "` in the column."); + + Frame fr2 = new MRTask2() { + @Override public void map(Chunk c, NewChunk nc) { + for (int i=0;i