如何一次将多个列强制转换为 H2OFrame 对象的因子?

How to Coerce multiple columns to factors at once for H2OFrame object?

我正在尝试遵循问题的建议:,但它不适用于 H2OFrame 对象,例如:

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)

产生以下错误消息:

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

如果我尝试一个一个地强制执行,它会起作用。另一种解决方法是首先将 data.frame 作为因子强制转换,然后将其转换为 H2OFrame 对象,例如:

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

任何解释为什么会发生或任何更好的解决方法?

正确的方法是使用 H2OFrame apply() 函数,但是,这会产生与@MKR 提到的相同的错误。我创建了一张 JIRA 票证 here.

理论上,这应该可行:

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

目前,解决方法是:

for (col in cols) {
  data.hex[col] <- as.factor(data.hex[col])
}