r- caret package error- createDataParition no observation

孤街醉人 提交于 2019-12-12 17:15:27

问题


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")

The data frame data1 is clearly visible in my environment and has the expected observations. Any thought?


回答1:


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")

Now if I do:

> 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

It works great.

So just use data1[[1]] in your code in the CreateDataPartition call and it will work.




回答2:


I solved the same problem by changing the target data type from character to factor because the downsample function require the response should be factor type. Hope this will be helpful



来源:https://stackoverflow.com/questions/33282174/r-caret-package-error-createdataparition-no-observation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!