例如:
require(RevoScaleR)
# Create a data frame
set.seed(100)
myData = data.frame(x = 1:100, y = rep(c("a", "b", "c", "d"), 25),
z = rnorm(100), w = runif(100))
# Create a multi-block .xdf file from the data frame
inputFile = file.path(tempdir(), "testInput.xdf")
rxDataStep(inData = myData, outFile = inputFile, rowsPerRead = 50,
overwrite = TRUE)
# Square the values in the column "z"; this works fine
rxDataStep(inData = inputFile, outFile = inputFile, overwrite = TRUE,
transforms = list(z = z^2))
# Define a squaring function and try to use it to repeat the previous step:
myFun = function(x) x^2
rxDataStep(inData = inputFile, outFile = inputFile, overwrite = TRUE,
transforms = list(z = myFun(z)))
最后一步因错误
而崩溃Error in transformation function: Error in eval(expr, envir, enclos) : could not find function "myFun"
rxDataStep
的文档指出“与所有表达式一样,变换...可以使用expression
函数在函数调用之外定义。”但我不知道如何实现这个建议,也找不到一个例子。例如,以下内容不起作用:
myFun = expression(function(x) x^2)
rxDataStep(inData = inputFile, outFile = inputFile, overwrite = TRUE,
transforms = list(z = myFun(z)))
答案 0 :(得分:2)
您当然可以将表达式传递给在函数调用之外创建的transform
。
它看起来像这样:
myFun <- expression(
list(x2 = x^2,
z2 = z^2))
rxDataStep(inData = inputFile, outFile = inputFile, overwrite = TRUE,
transforms = myFun)
如果你想在第一个例子中传递一个函数,它看起来像这样:
myFun2 <- function(dataList){
dataList$x2 <- dataList$x^2
dataList$z2 <- dataList$z^2
dataList
}
rxDataStep(inData = inputFile, outFile = inputFile, overwrite = TRUE,
transformFunc = myFun2)
答案 1 :(得分:1)
不知道为什么会这样有效!
env <- new.env()
env$myFun <- function(x) x^2
rxDataStep(inData = inputFile, outFile = inputFile, overwrite = TRUE,
transforms = list(z = myFun(z)), transformEnvir=env)