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

前端 未结 24 1803
猫巷女王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 11:10

    There are numerous approaches to achieve data partitioning. For a more complete approach take a look at the createDataPartition function in the caTools package.

    Here is a simple example:

    data(mtcars)
    
    ## 75% of the sample size
    smp_size <- floor(0.75 * nrow(mtcars))
    
    ## set the seed to make your partition reproducible
    set.seed(123)
    train_ind <- sample(seq_len(nrow(mtcars)), size = smp_size)
    
    train <- mtcars[train_ind, ]
    test <- mtcars[-train_ind, ]
    

提交回复
热议问题