Skip to content

Commit

Permalink
PUB-1153: set a level on a factor col
Browse files Browse the repository at this point in the history
  • Loading branch information
spennihana committed Mar 17, 2015
1 parent 5617c6e commit 69515eb
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
8 changes: 8 additions & 0 deletions R/h2o-package/R/Classes.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 22 additions & 0 deletions R/h2o-package/man/h2o.setLevel.Rd
Original file line number Diff line number Diff line change
@@ -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")
}
33 changes: 31 additions & 2 deletions src/main/java/water/exec/ASTOp.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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<c._len;++i)
nc.addNum(idx);
}
}.doAll(1, fr.anyVec()).outputFrame(null, fr.names(), fr.domains());
env.subRef(fr, skey);
env.poppush(1, fr2, null);
}
}

class ASTTrim extends ASTOp {
ASTTrim() { super(new String[]{"trim","x"},
new Type[]{Type.dblary(), Type.dblary()},
Expand Down

0 comments on commit 69515eb

Please sign in to comment.