使用插入符和数据表创建数据分区 [英] Creating a data partition using caret and data.table
问题描述
我有一个data.table在R,我想用caret包
set.seed(42)
trainingRows< -createDataPartition(DT $ variable,p = 0.75,list = FALSE)
head(trainingRows)#查看行号样本
但是,我不能选择与data.table的行。我必须转换为data.frame
DT_df <-as.data.frame(DT)
DT_train< -DT_df [trainingRows,]
dim(DT_train)
DT_train< - DT [。(trainingRows),]需要设置键。
除了转换为data.frame之外,还有更好的选择吗?
您可以简单地减少
trainingRows
的尺寸:
DT [trainingRows [,1]]
来自Bruce Pucci的 c()
函数也会减小尺寸。
data.frame很久以前被发现,最近我做了公关#1275 填补这个空白。
I have a data.table in R which I want to use with caret package
set.seed(42)
trainingRows<-createDataPartition(DT$variable, p=0.75, list=FALSE)
head(trainingRows) # view the samples of row numbers
However, I am not able to select the rows with data.table. Instead I had to convert to a data.frame
DT_df <-as.data.frame(DT)
DT_train<-DT_df[trainingRows,]
dim(DT_train)
the data.table alternative
DT_train <- DT[.(trainingRows),] requires the keys to be set.
Any better option other than converting to data.frame?
The reason is that createDataPartition
produces integer vector with two dimensions where the second could be losslessly dropped.
You can simply reduce dimension of trainingRows
using below:
DT[trainingRows[,1]]
The c()
function from Bruce Pucci's answer will reduce dimension too.
This minor difference vs. data.frame was spotted long time ago and recently I've made PR #1275 to fill that gap.
这篇关于使用插入符和数据表创建数据分区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!