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

前端 未结 24 1973
猫巷女王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条回答
  •  闹比i
    闹比i (楼主)
    2020-11-22 10:55

    My solution shuffles the rows, then takes the first 75% of the rows as train and the last 25% as test. Super simples!

    row_count <- nrow(orders_pivotted)
    shuffled_rows <- sample(row_count)
    train <- orders_pivotted[head(shuffled_rows,floor(row_count*0.75)),]
    test <- orders_pivotted[tail(shuffled_rows,floor(row_count*0.25)),]
    

提交回复
热议问题