我正在尝试使用函数将数据拆分为三个独立的数据帧(训练,测试,验证),但它不会返回我需要的结果。
这是我的功能:
splitData <- function(type) {
set.seed(1337)
rowTrain <- createDataPartition(y = cleaned.data$CHURN, p = 0.7, list = FALSE)
bufferDF <- cleaned.data[-rowTrain,]
rowTest <- createDataPartition(y = cleaned.data$CHURN, p = 0.50, list = FALSE)
if(type == "train") {cdTrain <- cleaned.data[rowTrain,]}
if(type == "train") {cdTrain}
if(type == "test") {cdTest <- cleaned.data[rowTest,]}
if(type == "test") {cdTest}
if(type == "validate") {cdValidate <- bufferDF[-rowTest,]}
if(type == "validate") {cdValidate}
}
你能否对我出错的地方有所了解?
干杯
答案 0 :(得分:1)
函数missing()
检查参数是否传递给它所在的函数。传递像train=="y"
这样的东西是没有意义的,因为train=="y"
不是函数splitData的参数。如果您在执行某项操作之前尝试确保各种变量已通过,则应为if(!missing(train))
。
但是,我不确定你的功能希望实现什么 - 它实际上并没有使用它收到的任何参数,除了检查它们是否存在......
更新:
试试这个:
splitData <- function(type) {
set.seed(1337)
rowTrain <- createDataPartition(y = cleaned.data$CHURN, p = 0.7, list = FALSE)
bufferDF <- cleaned.data[-rowTrain,]
rowTest <- createDataPartition(y = cleaned.data$CHURN, p = 0.50, list = FALSE)
if(type == "train") {cdTrain <- cleaned.data[rowTrain,]
return(cdTrain)}
if(type == "test") {cdTest <- cleaned.data[rowTest,]
return(cdTest)}
if(type == "validate") {cdValidate <- bufferDF[-rowTest,]
return(cdValidate)}
}
请注意&#34;验证&#34;将为您提供一个非常短的列表,因为您使用了从shorted bufferDF上的完整数据集创建的-rowTest,它只包含30%的数据集。您可能希望将定义rowTest的行替换为:
rowTest <- createDataPartition(y = bufferDF, p = 0.50, list = FALSE)
这将为您提供50%的测试数据样本。