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

前端 未结 24 1455
猫巷女王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:04

    I would use dplyr for this, makes it super simple. It does require an id variable in your data set, which is a good idea anyway, not only for creating sets but also for traceability during your project. Add it if doesn't contain already.

    mtcars$id <- 1:nrow(mtcars)
    train <- mtcars %>% dplyr::sample_frac(.75)
    test  <- dplyr::anti_join(mtcars, train, by = 'id')
    

提交回复
热议问题