2

I am trying to follow the suggestion on question: "Coerce multiple columns to factors at once", but it does not work for an H2OFrame object, for example:

data <- data.frame(matrix(sample(1:40), 4, 10, dimnames = list(1:4, LETTERS[1:10])))
data.hex <- as.h2o(data, destination_frame = "data.hex")
cols <- c("A", "C", "D", "H")
data.hex[cols] <- lapply(data.hex[cols], factor)

Produces the following error message:

Error in `[<-.H2OFrame`(`*tmp*`, cols, value = list(1L, 1L, 1L, 1L, 1L,  : 
  `value` can only be an H2OFrame object or a numeric or character vector
In addition: 
Warning message:
In if (is.na(value)) value <- NA_integer_ else if (!is.numeric(value) &&  :


the condition has length > 1 and only the first element will be used

If I try to coerce as factor one by one, it works. Another workaround is to coerce as factor first the data.frame, then convert it into H2OFrame object, for example:

data[cols] <- lapply(data[cols], factor)
data.hex <- as.h2o(data, destination_frame = "data.hex")

Any explanation why it happens or any better workaround?

David Leal
  • 6,373
  • 4
  • 29
  • 56
  • 1
    Give it a try as `apply(data.hex[cols], 2, factor)`. I dont see `lapply` supporting `H2OFrame` objects. – MKR Mar 20 '18 at 20:37
  • @MKR it doesn't work, I get the following error: `Error in .process.stmnt(stmnt, formalz, envs) : Don't know what to do with statement: is.null x` – David Leal Mar 20 '18 at 20:41
  • 1
    Yes. You are correct. All other functions like `sum`, `mean` etc works but `as.factor` gives error as `Don't know what to do with statement: is.H2OFrame ` for me. – MKR Mar 20 '18 at 20:48

1 Answers1

3

The right way to do it is to use the H2OFrame apply() function, however, this produces the same error that @MKR mentioned. I have created a JIRA ticket here.

In theory, this should work:

data.hex[,cols] <- apply(X = data.hex[,cols], MARGIN = 2, FUN = as.factor)

For now, the workaround is:

for (col in cols) {
  data.hex[col] <- as.factor(data.hex[col])
}
Erin LeDell
  • 8,704
  • 1
  • 19
  • 35