【发布时间】: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 -
是的。你是对的。
sum、mean等所有其他功能都有效,但as.factor给我的错误为Don't know what to do with statement: is.H2OFrame。