将数据集划分为 60%、20%、20%

use*_*635 4 validation split r dataset training-data

我正在尝试从 2 组数据移动到 3 组数据,如上述问题中所述。以下是我使用的脚本:

set.seed(125)
d <- sample(x = nrow(db), size = nrow(db) * 0.60, )
train60 <-db[d, ]
valid40 <-db[-d, ]
Run Code Online (Sandbox Code Playgroud)

有没有办法修改上面的脚本?我试图创建另一行:

valid40 <- db[-d] * 0.2 这不起作用。

当前数据集有几个因子变量。

我曾尝试在函数上使用Frank 的解决方案cut,但不知何故我设法得到

cut.default(seq(nrow(df)), nrow(df) * cumsum(c(0, spec)), labels = names(spec)) 中的错误:'breaks' 和 'labels' 的长度不同

即使在网上搜索帮助后我也不明白。

PKu*_*mar 5

如果我理解正确,那么您需要不重复的样本的 60%、20% 和 20% 的分叉。我以虹膜数据为例,其中包含 150 行和 5 列。

samp <- sample(1:nrow(iris),.6*nrow(iris)) ##60 and 40 bifurcation

train60 <- iris[samp,] ## This is the 60% chunk
remain40 <- iris[-samp,]  ## This is used for further bifurcation

samp2 <- sample(1:nrow(remain40),.5*nrow(remain40))

first20 <- remain40[samp2,] ## First chunk of 20%
secnd20 <- remain40[-samp2,] ## Second Chunk of 20%

Reduce("intersect",list(train60,first20,secnd20)) ##Check to find if there is any intersect , 0 rows means everything is fine and sample are not repetitive.
Run Code Online (Sandbox Code Playgroud)