Creating data partitions using carriage and data.table

I have data.table in R that 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 cannot select rows with data.table. Instead, I had to convert to data.frame

DT_df <-as.data.frame(DT)
DT_train<-DT_df[trainingRows,]
dim(DT_train)

alternative to data.table

DT_train <- DT[.(trainingRows),] requires the keys to be set.

Any better option besides converting to data.frame?

+4
source share
2 answers

Create your own

inTrain <- sample(MyDT[, .I], floor(MyDT[, .N] * .75))
Train <- MyDT[inTrain]
Test <- MyDT[-inTrain]

Or with Caret, you can just wrap trainingRowswith c ().

 trainingRows<-createDataPartition(DT$variable, p=0.75, list=FALSE)
 Train <- DT[c(trainingRows)]
 Test <- DT[c(-trainingRows)]

===

Edit Matt was about to add a comment, but too long.

, sample(.I,...) . , ( ) .I , 1:nrow(DT). , R sample() . . sample(nrow(DT)) , .I. . ?sample.

, , . .

, :

inTrain <- sample(MyDT[, .I], floor(MyDT[, .N] * .75))

:

inTrain <- MyDT[,sample(.N, floor(.N*.75))]
+3

, createDataPartition , .
trainingRows, :

DT[trainingRows[,1]]

c() .

data.frame , PR # 1275, .

+3

Source: https://habr.com/ru/post/1606845/


All Articles