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

前端 未结 24 1821
猫巷女王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条回答
  •  Happy的楠姐
    2020-11-22 11:05

    This is almost the same code, but in more nice look

    bound <- floor((nrow(df)/4)*3)         #define % of training and test set
    
    df <- df[sample(nrow(df)), ]           #sample rows 
    df.train <- df[1:bound, ]              #get training set
    df.test <- df[(bound+1):nrow(df), ]    #get test set
    

提交回复
热议问题