Skip to content

Instantly share code, notes, and snippets.

@estebanz01
Created May 11, 2015 16:05
Show Gist options
  • Save estebanz01/340cb56b6303882f61c7 to your computer and use it in GitHub Desktop.
Save estebanz01/340cb56b6303882f61c7 to your computer and use it in GitHub Desktop.
Split function to divide data frames in trainset & testset.
splitdf <- function(dataframe, seed=NULL) {
if (!is.null(seed)) set.seed(seed)
index <- 1:nrow(dataframe)
trainindex <- sample(index, trunc(length(index)/2))
trainset <- dataframe[trainindex, ]
testset <- dataframe[-trainindex, ]
list(trainset=trainset,testset=testset)
}
#Source of this function: http://www.gettinggeneticsdone.com/2011/02/split-data-frame-into-testing-and.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment