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

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

    If you type:

    ?sample
    

    If will launch a help menu to explain what the parameters of the sample function mean.

    I am not an expert, but here is some code I have:

    data <- data.frame(matrix(rnorm(400), nrow=100))
    splitdata <- split(data[1:nrow(data),],sample(rep(1:4,as.integer(nrow(data)/4))))
    test <- splitdata[[1]]
    train <- rbind(splitdata[[1]],splitdata[[2]],splitdata[[3]])
    

    This will give you 75% train and 25% test.

提交回复
热议问题