Reputation: 23231
In R I can take a random sample of half the rows of a dataframe like this:
data(iris); model_data <- iris
data <- model_data
half <- (nrow(model_data))%/%2
train <- model_data[sample(nrow(model_data), half), ]
However, I also need a simple way to take the rows that were not sampled and put them in another object called val
for validation.
Upvotes: 1
Views: 697
Reputation: 99371
You could write a function to keep train
and val
together, maybe something like
foo <- function(data) {
samp <- sample(nrow(data), nrow(data)/2)
list(train = data[samp,], val = data[-samp,])
}
foo(mtcars)
foo(iris)
Upvotes: 1