RobertF
RobertF

Reputation: 904

How to create a cross-validation fold variable via createFolds function in caret?

How do I create a variable, fold, in my analysis dataset which is assigned the values of 1:k from the createFolds cross-validation function in caret?

For example using the following college admissions dataset:

# Load data.
mydata <- read.csv("http://www.ats.ucla.edu/stat/data/binary.csv")
folds <- createFolds(mydata$admit, k=5)

# Create variable "mydata$fold" and assign values of 1:5 to mydata$fold.
? 

Upvotes: 0

Views: 3716

Answers (1)

jamieRowen
jamieRowen

Reputation: 1549

you can do this with createFolds, it has an argument list which defaults to TRUE giving a list of the indicies in each fold. If you change it to FALSE the function returns a vector of the folds that each row is being assigned to.

folds <- createFolds(mydata$admit, k=5,list = FALSE)

See ?createFolds for more details.

Upvotes: 2

Related Questions