r - 如何为 H2OFrame 对象一次强制多个列为因子?

标签 r h2o

我正在尝试遵循有关问题的建议:"Coerce multiple columns to factors at once" ,但它不适用于 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])
}

关于r - 如何为 H2OFrame 对象一次强制多个列为因子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49393343/

相关文章:

python - 预测类别或类别概率?

docker - 将 python 与在 docker 中运行的 h2o 连接,但 h2o 的 ipv4 发生变化

R Shiny 更改配置文件位置

r - h2o.predict函数中没有显示执行过程

r - 从 RStudio 启动 h2o 时出现问题,运行命令 'curl ' http ://localhost:5432 1'' had status 127

r - 使用 R 和 H2O 进行 Word2Vec 情感分类

r - 在 R 中的公式内使用 package::function 会给出不同的结果(特别是 coxph 内的survival::strata)

r - 将 geom_line 与多个分组一起使用

html - 如何使用 R 中的 rvest 包提取 <tag> 之外的文本

r - 计算因式时间变量之间的月份