【问题标题】:How to Coerce multiple columns to factors at once for H2OFrame object?如何为 H2OFrame 对象一次强制多个列转换为因子?
【发布时间】:2018-03-20 20:13:13
【问题描述】:

我正在尝试遵循有关问题的建议:"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")

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

【问题讨论】:

  • 试试apply(data.hex[cols], 2, factor)。我没有看到 lapply 支持 H2OFrame 对象。
  • @MKR 它不起作用,我收到以下错误:Error in .process.stmnt(stmnt, formalz, envs) : Don't know what to do with statement: is.null x
  • 是的。你是对的。 summean 等所有其他功能都有效,但 as.factor 给我的错误为 Don't know what to do with statement: is.H2OFrame

标签: r h2o


【解决方案1】:

正确的方法是使用 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])
}

【讨论】:

    猜你喜欢
    • 2016-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-28
    • 1970-01-01
    • 2012-05-30
    • 1970-01-01
    • 2015-01-29
    相关资源
    最近更新 更多