r- caret 包错误- createDataParition 没有观察 [英] r- caret package error- createDataParition no observation
问题描述
当我尝试在插入符号中运行 createDataPartition
时出现以下错误.
I'm getting the following error when I try to run createDataPartition
in caret.
Error in createDataPartition(data1, p = 0.8, list = FALSE) :
y must have at least 2 data points
我昨晚运行了完全相同的代码,没有任何错误.有什么想法吗?
I ran the same exact same code last night with no errors. Any thoughts?
predictors<- with(df, data.frame(xvar, xvar, xvar, xvar))
data1<-with(dfu2, data.frame(data1))
library(caret)
set.seed(1)
trainingRows<- createDataPartition(data1,
p=.80,
list=FALSE)
> dput(head(data1, 15)) structure(list(data1 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 1L)), .Names = "data1", row.names = c(NA, 15L), class = "data.frame")
数据框 data1
在我的环境中清晰可见并且具有预期的观察结果.有什么想法吗?
The data frame data1
is clearly visible in my environment and has the expected observations. Any thought?
推荐答案
这不起作用,因为 data1
在你的情况下是一个 data.frame 而它应该是一个向量,因为它在文档中提到?createDataPartition
.看这个例子:
This does not work because data1
is a data.frame in your case whereas it should be a vector as it is mentioned the documentation of ?createDataPartition
. See this example:
#using your data
data1 <- structure(list(data1 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 1L)), .Names = "data1", row.names = c(NA, 15L), class = "data.frame")
现在如果我这样做:
> createDataPartition(data1)
Error in createDataPartition(data1) : y must have at least 2 data points
我遇到和你一样的错误.然而,如果它是一个向量:
I get the same error as you. Whereas, if it is a vector:
> createDataPartition(data1[[1]] )
$Resample1
[1] 1 2 3 4 8 9 12 15
效果很好.
因此,只需在 CreateDataPartition
调用中的代码中使用 data1[[1]]
即可.
So just use data1[[1]]
in your code in the CreateDataPartition
call and it will work.
这篇关于r- caret 包错误- createDataParition 没有观察的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!