How to split data into training/testing sets using sample function

前端 未结 24 2047
猫巷女王i
猫巷女王i 2020-11-22 10:43

I\'ve just started using R and I\'m not sure how to incorporate my dataset with the following sample code:

sample(x, size, replace = FALSE, prob = NULL)
         


        
24条回答
  •  广开言路
    2020-11-22 10:43

    Use base R. Function runif generates uniformly distributed values from 0 to 1.By varying cutoff value (train.size in example below), you will always have approximately the same percentage of random records below the cutoff value.

    data(mtcars)
    set.seed(123)
    
    #desired proportion of records in training set
    train.size<-.7
    #true/false vector of values above/below the cutoff above
    train.ind<-runif(nrow(mtcars))

提交回复
热议问题